WoW
In the Central Park Zoo of New York, the game Wonders of Worlds, a massively multiplayer online role-playing game, is a favorite among its inhabitants. The game world is an infinite plane divided into identical unit-sized cells, each defined by two integer coordinates, (x) and (y). One of the most thrilling aspects for players at the highest level (85th) is the player-versus-player battles in a special tournament arena. This arena is a rectangle made up of (n m) cells. The bottom-left corner of the arena is at cell ((0, 0)), and the top-right corner is at cell ((n-1, m-1)). Only one player can occupy a cell at any given time. Players take turns, and during a turn, a player can either attack an opponent or move to another sector of the arena.
Today, Rico, playing as the Angel of Death, has challenged Skipper, who plays as a mage. In WoW, the challenger moves second, so Skipper will make the first move.
Before entering the arena, Kowalski, a member of Rico's gaming guild, informed Skipper that Rico discovered a critical bug in the game engine, allowing him to instantly defeat an opponent with full health using a single ability. To avoid a swift defeat, Skipper's only option is to attack Rico.
The spell system for mages in WoW is unique. Each mage has a set of (k) basic spells. Each basic spell (i) consists of a sequence of movements, defined by a set of (d_i) vectors with integer coordinates. When a basic spell is cast, it starts from a specific point and moves sequentially according to the vectors in the set. After completing all movements, the spell explodes, dealing non-zero damage if the opponent is at the endpoint.
A mage can combine different basic spells. A combination involves applying basic spells sequentially. After the first spell's movements are completed, it doesn't explode; instead, the second spell begins its movements from the endpoint of the first spell, and so on. After the last spell in the combination, the spell explodes at the endpoint.
The battle arena is surrounded by an impenetrable anti-magic barrier. Therefore, a basic spell can only be applied if its movements stay within the arena's boundaries. Similarly, a combination can only be applied if all basic spells within it can be applied in the specified order.
Given the large number of possible combinations, Skipper asks for your help in finding a combination of basic spells, with a total number of no more than (10^6), that can be applied consecutively to damage Rico, or to determine if it's impossible.
It is guaranteed that if a solution exists, it does not require more than (10^6) basic spells.
Input
The first line contains three integers (n), (m), and (k) ((1 n, m 1000), (1 k 50)) — the dimensions of the arena and the number of basic spells. The second line contains four integers (x_1), (y_1), (x_2), (y_2) ((0 x_i n), (0 y_i m), ((x_1, y_1) (x_2, y_2))) — the coordinates of Skipper and Rico on the arena. The next (k) lines describe the basic spells. The ((i+2))-th line contains an integer (d_i) ((1 d_i 5 10^5)) — the number of movements in the basic spell. This is followed by (d_i) pairs of integers (x_j), (y_j) ((-n < x_j < n), (-m < y_j < m)) — the coordinates of the vectors defining the movements. The total number of vectors across all basic spells does not exceed (5 10^5). All numbers are on one line, separated by a single space.
Output
If a combination of no more than (10^6) basic spells can achieve the goal, output the integer (p) — the number of basic spells in the combination — on the first line. On the second line, output (p) numbers, separated by a space — the indices of the basic spells in the order of their application. Basic spells are numbered starting from one, in the order they are described in the input. If multiple solutions exist, output any.
If no solution exists, output (-1) on the first line.