This page is still under construction.

Parts of this page are still being built. What you see may change.

Find the Double Class

Time limit1sMemory limit256 MB

Summary
Given M distinct rows of N section numbers, output at most M new rows so that each input row agrees with some new row in every column, yet no new row equals an input row.
Level

Hard8 of 10

Topics
Combinatorics, Math, Greedy, Implementation
Solved
No attempts yet

Problem

MM members of Cycom are all taking the same courses this year, and the number of courses they take is NN. Even for the same course there are several sections, and only people in the same section attend the lecture together. Surprisingly, all MM members are in different sections, so no pair of them attends even one class together.

To escape loneliness, the Cycom members decide to create KK imaginary friends. Each friend will take the same set of courses as the Cycom members, and their sections can be chosen freely. The goal is to make every person attend every course together with an imaginary friend.

KK can be chosen freely, but an imaginary friend must not outnumber real humans, so K≤MK \le M must hold. Also, for each Cycom member, there must not exist an imaginary friend whose course section numbers all match exactly.

Input

The first line gives the number of courses NN and the number of members MM.

From the second line to the M+1M+1-th line, the i+1i+1-th line gives the integers Ai,1,Ai,2,⋯ ,Ai,NA_{i, 1}, A_{i,2}, \cdots, A_{i, N}. Ai,jA_{i, j} is the section number of course jj taken by member ii.

Output

The first line prints the number of imaginary friends KK.

From the second line to the K+1K+1-th line, the i+1i+1-th line prints the integers Bi,1,Bi,2,⋯ ,Bi,NB_{i,1}, B_{i,2}, \cdots, B_{i,N}. Bi,jB_{i,j} is the section number of course jj taken by imaginary friend ii.

Constraints

  • 2≤N≤10002 \le N \le 1000
  • 2≤M≤10002 \le M \le 1000
  • For all 1≤i≤N1 \le i \le N and 1≤x<y≤M1 \le x < y \le M, Ax,i≠Ay,iA_{x,i} \neq A_{y,i}. (In other words, the Cycom members have no overlapping sections in any course.)
  • 1≤Ai,j≤M1 \le A_{i,j} \le M
  • 0≤K≤M0 \le K \le M
  • 1≤Bi,j≤M1 \le B_{i,j} \le M
  • For all 1≤i≤M1 \le i \le M and 1≤j≤K1 \le j \le K, there must exist at least one xx with 1≤x≤N1 \le x \le N such that Ai,x≠Bj,xA_{i, x} \neq B_{j, x}. (In other words, for each Cycom member, there must not exist an imaginary friend whose course section numbers all match exactly.)
  • For all 1≤i≤M1 \le i \le M and 1≤j≤N1 \le j \le N, there must exist at least one xx with 1≤x≤K1 \le x \le K such that Ai,j=Bx,jA_{i, j} = B_{x, j}. (In other words, each Cycom member must be able to attend every course together with at least one imaginary friend.)

Examples1

  1. Example 1

    Input
    3 3
    1 1 1
    2 2 2
    3 3 3
    
    Expected output
    3
    1 2 3
    2 3 1
    3 1 2