#P9070. Cirno's Card Exchange

    ID: 22229 Type: Default 1000ms 256MiB

Cirno's Card Exchange

Cirno's Card Exchange

Due to a strange mutation, Cirno discovered that the cards which sealed her abilities are circulating freely in Gensokyo. There are \(n\) different types of cards and for each type there are exactly \(n\) copies. Moreover, there are \(n\) people and each person has bought exactly \(n\) cards (possibly with duplicates).

Cirno wishes to achieve a state where every person has exactly one copy of each card type. To do so, she arranges an exchange process. In every exchange, she picks two persons and instructs them to swap one card each. However, due to the diminishing magical power on each card as a result of the exchange, each card can be involved in at most one swap.

In an exchange between person \(u\) and person \(v\), if person \(u\) gives a card of type \(x\) and receives a card of type \(y\) (and similarly, person \(v\) gives type \(y\) and receives type \(x\)), then it must be that person \(u\) originally has at least two copies of card \(x\) and does not have any card of type \(y\), and person \(v\) has at least two copies of card \(y\) and is missing card type \(x\).

Your task is to output a sequence of such exchanges that leads to every person holding exactly one card of each type, or to report that no solution exists (if such a sequence is impossible under the constraints).

inputFormat

The first line contains an integer \(n\). The next \(n\) lines each contain \(n\) integers. The \(j\)-th number on the \(i\)-th line denotes the card type that the \(i\)-th person initially has. Card types are integers between 1 and \(n\) (inclusive).

outputFormat

If a valid exchange sequence exists, print the number of exchanges \(k\) on the first line, and then \(k\) lines each containing four integers \(u\), \(v\), \(x\), and \(y\). This means that person \(u\) and person \(v\) exchange a card of type \(x\) and a card of type \(y\) respectively. If no valid sequence exists, output -1.

sample

2
1 1
2 2
1

1 2 1 2

</p>