ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules

[Online] ICPC Ural Regional Contest Qualifier 2026

About     Problems     Submit solution     Judge status     Standings
Contest is over

H. Puzzles for an Enthusiast

Time limit: 2.0 second
Memory limit: 256 MB
Yamochka is a big fan of puzzles, so recently she bought herself a new jigsaw puzzle. The assembled puzzle is a rectangular grid of K rows and L columns, consisting of K · L equal-sized squares, called puzzle pieces. Each side of a piece can have a socket of a certain type, or a tab of a certain type, or be smooth if that side lies on the boundary of the puzzle. If two pieces in the assembled puzzle are adjacent by a side, then on that side one piece must have a tab and the other a socket, and they must be of the same type.
Unfortunately, Yamochka turned out to be not very experienced at assembling puzzles, so she bought a beginner set: in this set, all tab types are distinct, and all socket types are distinct as well. More formally: each connection type corresponds to exactly one tab and one socket, or to nothing.
After opening the box with the puzzle and trying to fit some pieces together, Yamochka realized that puzzles are not really her thing, so the task of assembling this puzzle falls entirely on you.

Input

The first line contains an integer N (1 ≤ N ≤ 105) — the number of puzzle pieces.
The next N lines contain four integers li, ui, ri, di (−106 ≤ li, ui, ri, di ≤ 106) — the descriptions of the left, top, right, and bottom sides of the i-th puzzle piece, respectively. If t is one of these numbers, then:
  • if t > 0, then this side has a tab of type |t|;
  • if t < 0, then this side has a socket of type |t|;
  • if t = 0, then this side is smooth and must lie on the outer boundary of the puzzle.
Of course, any piece can be rotated clockwise by any angle that is a multiple of 90∘.

Output

In the first line, output two numbers K, L (1 ≤ K, L ≤ N; K × L = N).
In the next K lines, output L integers — the indices of the pieces in input order that should be placed in those positions in the assembled puzzle.
Each piece must appear in the output exactly once. The rotations of the pieces do not need to be specified in the output. The answer is considered correct if there exists a set of rotations such that all adjacent sides are compatible and all smooth sides lie on the boundary.
It is guaranteed that an answer always exists. If there are multiple valid answers, output any of them.

Sample

inputoutput
6
0 0 -1 4
1 0 5 3
-5 0 0 -6
7 6 0 0
2 -3 -7 0
0 -4 -2 0
2 3
1 2 3
6 5 4
Problem Author: Yackov Malin
Problem Source: The 2026 ICPC Ural Qualification