#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pi 3.14159265358979323846
#define sqr(a) (a)*(a)
#define v vector
#define ll long long
#define ld long double
#define f first
#define sec second
#define pb push_back
#define pf push_front
#define umap unordered_map
#define sort(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define mod 1000000007
#define inf 2*1e18

using namespace std;
using namespace __gnu_pbds;

typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>uset;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    srand(time(NULL));
    ll n,m,i,j,k,p,t,x,y;
    cin>>n>>m;
    k=n*m;
    v<ll>a(k);
    v<pair<ll,ll>>res;
    umap<ll,umap<ll,ll>>cnt;
    umap<ll,umap<ll,set<ll>>>st;
    for (i=0;i<k;i++){
        cin>>a[i];
        a[i]--;
    }
    for (i=0;i<n;i++){
        for (j=0;j<m;j++){
            cnt[i][a[i*m+j]]++;
            st[i][a[i*m+j]].insert(i*m+j);
        }
    }
    for (i=0;i<m;i++){
        for (j=i+1;j<m;j++){
            for (p=0;p<n;p++){
                for (t=0;t<n;t++){
                    if (p!=t){
                        if (cnt[p][i]>1 && cnt[t][j]>1 && cnt[t][i]==0 && cnt[p][j]==0){
                            cnt[p][i]--;
                            cnt[t][j]--;
                            cnt[t][i]++;
                            cnt[p][j]++;
                            x=*st[p][i].begin();
                            y=*st[t][j].begin();
                            st[p][i].erase(x);
                            st[t][j].erase(y);
                            res.pb({30,x});
                            res.pb({x,y});
                            res.pb({30,y});
                            st[t][i].insert(y);
                            st[p][j].insert(x);
                        }
                    }
                }
            }
        }
    }
    cout<<res.size()<<endl;
    for (auto pa : res){
        cout<<pa.f<<' '<<pa.sec<<endl;
    }
    return 0;
}
