program Z4;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  n,m,i,p,j,f,k,i1,j1:LongInt;
  a:array [1..160001] of LongInt;
  r,g:array [0..400] of LongInt;
  value:array[1..160000,1..2] of LongInt;

function need(num:LongInt):LongInt;

var
  i,j,f:LongInt;
  p:array [0..400] of LongInt;
begin
  FillChar (p,SizeOf(p),0);
  i:=(num-1) div m;
  for j:=1 to m do
    begin
      p[a[i*m+j]]:=p[a[i*m+j]]+1;
//      if p[a[i*m+j]]=0 then p[a[i*m+j]]:=1
//      else
//        begin
//          f:=a[i*m+j];
//          Break;
//        end;
    end;
  for i:=1 to m do
    if p[i]=0 then
      begin
        f:=i;
        break;
      end;
  need:=f;
end;

function find(num:LongInt):LongInt;
var
  i,j,f,o:LongInt;
begin
  o:=0;
  for i:=1 to n do
    if r[i]=0 then
      begin
        f:=0;
        for j:=1 to m do
          begin
            if a[i*m+j] = num then f:=f+1;
            if f>1 then
              begin
                o:=i*m+j;
                Break;
              end;
          end;
        if o<>0 then
          Break;
      end;
  if o = 0 then o:=m*n+1;
  find:=o;
end;

function check(num:longint):LongInt;
//var
//  i1,j,f1:LongInt;
begin
  f:=1;
  for i1:=0 to n-1 do
    if r[i1]=0 then
      begin
        FillChar (g,SizeOf(g),0);
        for j1:=1 to m do
          begin
            i:=i1*m+j1;
            g[a[i]]:=g[a[i]]+1;
            if g[a[i]]>1 then
              begin
                f:=0;
                a[n*m+1]:=a[i];
                a[i]:=0;
                k:=k+1;
                value[k,1]:=i;
                value[k,2]:=n*m+1;
                while i <> n*m+1 do
                  begin
                    j:=find(need(i));
                    a[i]:=a[j];
                    a[j]:=0;
                    k:=k+1;
                    value[k,1]:=j;
                    value[k,2]:=i;
                    //Writeln (j,' ',i);
                    i:=j;
                  end;
              end;
            if f = 0 then
              Break;
          end;
        if f = 0 then
          Break;
        if f = 1 then
          r[i1]:=1;
      end;
  if f = 0 then
    check(1);
end;

begin
  Assign (Input,'input.txt');
  reset (Input);
  Assign (Output,'output.txt');
  Rewrite (Output);
  Read (n,m);
  for i:=1 to n*m do
    read (a[i]);
  check(1);
  Writeln (k);
  for i:=1 to k do
    Writeln (value[i,1],' ',value[i,2]);
  Close (Input);
  Close (Output);
  { TODO -oUser -cConsole Main : Insert code here }
end.