#include <bits/stdc++.h>
using namespace std;
vector<int>vs;
vector<double>outs;
int finals[200005];
int main(){
    int n,t,x,y,c = 0;
    long long  total = 0;
    cin >> n;
    vs.push_back(0);

    for(int i = 0;i < n;i++){
        cin >> t;
        if(t == 1){
            cin >> x >>y;
   //         for(int i = 0;i < x;i++)vs[i]+=y;
            finals[x-1]+=y;
            total+=(y*x);
            }else if(t == 2){
                cin >> x;
                vs.push_back(x);
                total+=x;
            }else {
                 total-=(vs.back()+finals[vs.size()-1]);
              finals[vs.size()-2] += finals[vs.size()-1];
                 vs.pop_back();
            }
//cout<<"** ";
     //     for(int j = 0;j < vs.size();j++)c+=vs[j],cout<<vs[j]<<' ';
  //              cout<<endl<<c<<" "<<vs.size()<<" "<<(double)(c/vs.size())<<endl;
//            printf("%.10lf\n",(double)total/(double)vs.size());
printf("%.10lf\n", (double)(total) / (double)(vs.size()));
            c = 0;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<int>vs;
vector<double>outs;
int finals[200005];
int main(){
    int n,t,x,y,c = 0;
    long long  total = 0;
    cin >> n;
    vs.push_back(0);
int s = 1;
    for(int i = 0;i < n;i++){
        cin >> t;
        if(t == 1){
            cin >> x >>y;
   //         for(int i = 0;i < x;i++)vs[i]+=y;
            finals[x]+=y;
            total+=(y*x);
            }else if(t == 2){
                cin >> x;
                vs.push_back(x);
                total+=x;
                s++;
            }else {
                 total-=(vs.back()+finals[s]);
              finals[s-1] += finals[s];
                 vs.pop_back();
                 s--;
            }
//cout<<"** ";
     //     for(int j = 0;j < vs.size();j++)c+=vs[j],cout<<vs[j]<<' ';
  //              cout<<endl<<c<<" "<<vs.size()<<" "<<(double)(c/vs.size())<<endl;
//            printf("%.10lf\n",(double)total/(double)vs.size());
printf("%.10lf\n", (double)(total) / (double)(s));
            c = 0;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<int>vs;
vector<double>outs;
int finals[200005];
int main(){
    int n,t,x,y,c = 0;
    long long  total = 0;
    cin >> n;
    vs.push_back(0);

    for(int i = 0;i < n;i++){
        cin >> t;
        if(t == 1){
            cin >> x >>y;
   //         for(int i = 0;i < x;i++)vs[i]+=y;
            finals[x-1]+=y;
            total+=(y*x);
            }else if(t == 2){
                cin >> x;
                vs.push_back(x);
                total+=x;
            }else {
                 total-=(vs.back()+finals[vs.size()-1]);
              finals[vs.size()-2] += finals[vs.size()-1];
                 vs.pop_back();
            }
//cout<<"** ";
     //     for(int j = 0;j < vs.size();j++)c+=vs[j],cout<<vs[j]<<' ';
  //              cout<<endl<<c<<" "<<vs.size()<<" "<<(double)(c/vs.size())<<endl;
            printf("%.10llf\n",(double)total/(double)vs.size());
            c = 0;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<int>vs;
vector<double>outs;
int finals[200005];
int main(){
    int n,t,x,y,c = 0;
    long long  total = 0;
    cin >> n;
    vs.push_back(0);

    for(int i = 0;i < n;i++){
        cin >> t;
        if(t == 1){
            cin >> x >>y;
            finals[x-1]+=y;
            total+=(y*x);
            }else if(t == 2){
                cin >> x;
                vs.push_back(x);
                total+=x;
            }else {
                 total-=(vs.back()+finals[vs.size()-1]);
              finals[vs.size()-2] += finals[vs.size()-1];
              finals[vs.size()-1] = 0;
                 vs.pop_back();
            }
            printf("%.6llf\n",(double)total/(double)vs.size());
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<double>vs;
vector<double>outs;
double finals[200005];
int main(){
    int n,t,x,y,c = 0;
    double  total = 0.0;
    cin >> n;
    vs.push_back(0);
int s = 1;
    for(int i = 0;i < n;i++){
        cin >> t;
        if (t == 1) {
            int a, x;
            cin >> x >>y;
            finals[x]+=(double)y;
            total += (double)(y) * (double)(x);
        } else if (t == 2) {
            int k;
                cin >> x;
                vs.push_back((double)x);
                total+=(double)x;
        } else {
                 total-=(double)vs.back();
                 total-=(double)finals[vs.size()];
              finals[vs.size()-1] += finals[vs.size()];
                 vs.pop_back();
        }
       printf("%.10lf\n", (double)(total) / (double)(vs.size()));
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int INF = 0x3c3c3c3c;
const ll INFL = 0x3c3c3c3c3c3c3c3c;
const int MAX_N = 1e5 + 9;

int D[MAX_N];
vector<int> toDist[MAX_N];
vector<pair<int, int>> G;

int main(){
	cin.sync_with_stdio(false);
	cin.tie(NULL);
	cout.sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for(int i = 0; i < n; i++){
		cin >> D[i];
	}
	for(int i = 0; i < n; i++){
		toDist[D[i]].push_back(i);
	}
	if((int)toDist[0].size() != 1) return !printf("-1");
	queue<int> cq, nq;
	cq.push(toDist[0][0]);
	int ans = 0;
	for(int i = 1; i <= n; i++){
        int w = (i == 1 ? 0 : 1);
		if((ll)cq.size() * (k - w) < (ll)toDist[i].size()) return !printf("-1");
        if(cq.empty()) continue;
		int tdsz = toDist[i].size();
		int connected = 0;
		int curr = cq.front();
        cq.pop();
		for(int j = 0; j < tdsz; j++){
            if(connected < k - w){
			    G.push_back({curr + 1, toDist[i][j] + 1});
			    nq.push(toDist[i][j]);
                ans++;
                connected++;
            }
            if(connected >= k - w){
                connected = 0;
                if(!cq.empty()){
                    curr = cq.front();
                    cq.pop();
                }
            }
		}
		cq = nq;
		nq = queue<int>();
	}
	printf("%d\n", ans);
	for(auto t : G){
		printf("%d %d\n", t.first, t.second);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int INF = 0x3c3c3c3c;
const ll INFL = 0x3c3c3c3c3c3c3c3c;
const int MAX_N = 1e5 + 9;

int D[MAX_N];
vector<int> toDist[MAX_N];
vector<pair<int, int>> G;

int main(){
	cin.sync_with_stdio(false);
	cin.tie(NULL);
	cout.sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for(int i = 0; i < n; i++){
		cin >> D[i];
	}
	for(int i = 0; i < n; i++){
		toDist[D[i]].push_back(i);
	}
	if((int)toDist[0].size() != 1) return !printf("-1");
	int curr, prev;
	curr = prev = 1;
	for(int i = 1; i <= n; i++){
		curr = (int)toDist[i].size();
		if(prev == 0 && curr) return !printf("-1");
		prev = curr;
	}
	queue<int> cq, nq;
	cq.push(toDist[0][0]);
	int ans = 0;
	for(int i = 1; i <= n; i++){
        int w = (i != 1);
		if(1LL * (int)cq.size() * (k - w) < 1LL * (int)toDist[i].size()) return !printf("-1");
		int tdsz = toDist[i].size();
		int connected = w;
		int curr = cq.front();
		cq.pop();
		for(int j = 0; j < tdsz; j++){
			G.push_back({curr + 1, toDist[i][j] + 1});
			nq.push(toDist[i][j]);
			connected++;
			ans++;
			if(connected == k){
				connected = w;
				curr = cq.front();
				cq.pop();
			}
		}
		cq = nq;
		nq = queue<int>();
	}
	printf("%d\n", ans);
	for(auto t : G){
		printf("%d %d\n", t.first, t.second);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int INF = 0x3c3c3c3c;
const ll INFL = 0x3c3c3c3c3c3c3c3c;
const int MAX_N = 1e5 + 9;

int D[MAX_N];
vector<int> toDist[MAX_N];
vector<pair<int, int>> G;

int main(){
	cin.sync_with_stdio(false);
	cin.tie(NULL);
	cout.sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for(int i = 0; i < n; i++){
		cin >> D[i];
	}
	for(int i = 0; i < n; i++){
		toDist[D[i]].push_back(i);
	}
	if((int)toDist[0].size() != 1) return !printf("-1");
	queue<int> cq, nq;
	cq.push(toDist[0][0]);
	int ans = 0;
	for(int i = 1; i <= n; i++){
        int w = (i == 1 ? 0 : 1);
		if((ll)cq.size() * (k - w) < (ll)toDist[i].size()) return !printf("-1");
        if(cq.empty()) continue;
		int tdsz = toDist[i].size();
		int connected = 0;
		int curr = cq.front();
        cq.pop();
		for(int j = 0; j < tdsz; j++){
            if(connected < k - w){
			    G.push_back({curr + 1, toDist[i][j] + 1});
			    nq.push(toDist[i][j]);
                ans++;
                connected++;
            }
            if(connected >= k - w){
                connected = 0;
                if(!cq.empty()){
                    curr = cq.front();
                    cq.pop();
                }
                else return !printf("WTF!\n");
            }
		}
		cq = nq;
		nq = queue<int>();
	}
	printf("%d\n", ans);
	for(auto t : G){
		printf("%d %d\n", t.first, t.second);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int INF = 0x3c3c3c3c;
const ll INFL = 0x3c3c3c3c3c3c3c3c;
const int MAX_N = 1e5 + 9;

int D[MAX_N];
vector<int> toDist[MAX_N];
vector<pair<int, int>> G;

int main(){
	cin.sync_with_stdio(false);
	cin.tie(NULL);
	cout.sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for(int i = 0; i < n; i++){
		cin >> D[i];
	}
	for(int i = 0; i < n; i++){
		toDist[D[i]].push_back(i);
	}
	if((int)toDist[0].size() != 1) return !printf("-1");
	int curr, prev;
	curr = prev = 1;
	for(int i = 1; i <= n; i++){
		curr = (int)toDist[i].size();
		if(prev == 0 && curr) return !printf("-1");
		prev = curr;
	}
	queue<int> cq, nq;
	cq.push(toDist[0][0]);
	int ans = 0;
	for(int i = 1; i <= n; i++){
		if(1LL * cq.size() * k < toDist[i].size()) return !printf("-1");
		int tdsz = toDist[i].size();
		int connected = 0;
		int curr = cq.front();
		cq.pop();
		for(int j = 0; j < tdsz; j++){
			G.push_back({curr + 1, toDist[i][j] + 1});
			nq.push(toDist[i][j]);
			connected++;
			ans++;
			if(connected == k){
				curr = cq.front();
				cq.pop();
			}
		}
		cq = nq;
		nq = queue<int>();
	}
	printf("%d\n", ans);
	for(auto t : G){
		printf("%d %d\n", t.first, t.second);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int INF = 0x3c3c3c3c;
const ll INFL = 0x3c3c3c3c3c3c3c3c;
const int MAX_N = 1e5 + 9;

int D[MAX_N];
vector<int> toDist[MAX_N];
vector<pair<int, int>> G;

int main(){
	cin.sync_with_stdio(false);
	cin.tie(NULL);
	cout.sync_with_stdio(false);
	int n, k;
	cin >> n >> k;
	for(int i = 0; i < n; i++){
		cin >> D[i];
	}
	for(int i = 0; i < n; i++){
		toDist[D[i]].push_back(i);
	}
	if((int)toDist[0].size() != 1) return !printf("-1");
	int curr, prev;
	curr = prev = 1;
	for(int i = 1; i <= n; i++){
		curr = (int)toDist[i].size();
		if(prev == 0 && curr) return !printf("-1");
		prev = curr;
	}
	queue<int> cq, nq;
	cq.push(toDist[0][0]);
	int ans = 0;
	for(int i = 1; i <= n; i++){
        int w = (i == 1 ? 0 : 1);
		if(1LL * cq.size() * (k - w) < 1LL * toDist[i].size()) return !printf("-1");
        if(cq.empty()) continue;
		int tdsz = toDist[i].size();
		int connected = w;
		int curr = cq.front();
		cq.pop();
		for(int j = 0; j < tdsz; j++){
			G.push_back({curr + 1, toDist[i][j] + 1});
			nq.push(toDist[i][j]);
			connected++;
			ans++;
			if(connected == k){
				connected = w;
				curr = cq.front();
				cq.pop();
			}
		}
		cq = nq;
		nq = queue<int>();
	}
	printf("%d\n", ans);
	for(auto t : G){
		printf("%d %d\n", t.first, t.second);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N=100005;
int n,k,e[N],mx;
vector<int>g[N];

bool f(int b){
    if(e[0]!=1){ return 0; }

    for(int x=1 ; x<=b ; x++){
        ll c=(ll)(e[x-1]*(x==1 ? k : k-1));
        if(e[x]>c){ return 0; }
    }
    return 1;
}

void setup();
int main()
{
    setup();

    int a;
    cin>>n>>k;

    for(int x=1 ; x<=n ; x++){
        cin>>a;
        mx=max(mx,a);

        e[a]++;
        g[a].push_back(x);
    }

    if(f(mx)){
        cout<<n-1<<endl;

        for(int x=1 ; x<=mx ; x++){
            for(int y=0,z=0 ; y<e[x] ; y++,z++){
                if(z==e[x-1]){ z=0; }

                cout<<g[x-1][z]<<" "<<g[x][y]<<endl;
            }
        }
    }
    else{ cout<<-1; }

    return 0;
}
void setup()
{
    ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("test.in.txt","rt",stdin);
    //freopen("s.out.txt","wt",stdout);
#endif // ONLINE_JUDGE
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N=100005;
int n,k,e[N],mx;
vector<int>g[N];

bool f(int b){
    if(e[0]!=1){ return 0; }

    for(int x=1 ; x<=b ; x++){
        ll c=(ll)e[x-1]*(x==1 ? k : k-1);
        if(e[x]>c){ return 0; }
    }
    return 1;
}

void setup();
int main()
{
    setup();

    int a;
    cin>>n>>k;

    for(int x=1 ; x<=n ; x++){
        cin>>a;
        mx=max(mx,a);

        e[a]++;
        g[a].push_back(x);
    }

    if(f(mx)){
        cout<<n-1<<endl;

        for(int x=1 ; x<=mx ; x++){
            for(int y=0,z=0 ; y<e[x] ; y++,z++){
                if(z==e[x-1]){ z=0; }

                cout<<g[x-1][z]<<" "<<g[x][y]<<endl;
            }
        }
    }
    else{ cout<<-1; }

    return 0;
}
void setup()
{
    ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("test.in.txt","rt",stdin);
    //freopen("s.out.txt","wt",stdout);
#endif // ONLINE_JUDGE
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N=100005;
int n,k,e[N],mx;
vector<int>g[N];

bool f(int b){
    if(e[0]!=1){ return 0; }

    for(int x=1 ; x<=b ; x++){
        if(e[x]>e[x-1]*k){ return 0; }
    }
    return 1;
}

void setup();
int main()
{
    setup();

    int a;
    cin>>n>>k;

    for(int x=1 ; x<=n ; x++){
        cin>>a;
        mx=max(mx,a);

        e[a]++;
        g[a].push_back(x);
    }

    if(f(mx)){
        cout<<n-1<<endl;

        for(int x=1 ; x<=mx ; x++){
            for(int y=0,z=0 ; y<e[x] ; y++,z++){
                if(z==e[x-1]){ z=0; }

                cout<<g[x-1][z]<<" "<<g[x][y]<<endl;
            }
        }
    }
    else{ cout<<-1; }

    return 0;
}
void setup()
{
    ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("test.in.txt","rt",stdin);
    //freopen("s.out.txt","wt",stdout);
#endif // ONLINE_JUDGE
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N=100005;
int n,k,e[N],mx;
vector<int>g[N];

bool f(int b){
    if(e[0]!=1){ return 0; }

    for(int x=1 ; x<=b ; x++){
        int c=(x==1 ? k : k-1);
        if(e[x]>e[x-1]*c){ return 0; }
    }
    return 1;
}

void setup();
int main()
{
    setup();

    int a;
    cin>>n>>k;

    for(int x=1 ; x<=n ; x++){
        cin>>a;
        mx=max(mx,a);

        e[a]++;
        g[a].push_back(x);
    }

    if(f(mx)){
        cout<<n-1<<endl;

        for(int x=1 ; x<=mx ; x++){
            for(int y=0,z=0 ; y<e[x] ; y++,z++){
                if(z==e[x-1]){ z=0; }

                cout<<g[x-1][z]<<" "<<g[x][y]<<endl;
            }
        }
    }
    else{ cout<<-1; }

    return 0;
}
void setup()
{
    ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("test.in.txt","rt",stdin);
    //freopen("s.out.txt","wt",stdout);
#endif // ONLINE_JUDGE
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N=100005;
int n,k,e[N],mx;
vector<int>g[N];

bool f(int b){
    if(e[0]!=1){ return 0; }

    for(int x=1 ; x<=b ; x++){
        ll c=e[x-1]*(x==1 ? k : k-1);
        if(e[x]>c){ return 0; }
    }
    return 1;
}

void setup();
int main()
{
    setup();

    int a;
    cin>>n>>k;

    for(int x=1 ; x<=n ; x++){
        cin>>a;
        mx=max(mx,a);

        e[a]++;
        g[a].push_back(x);
    }

    if(f(mx)){
        cout<<n-1<<endl;

        for(int x=1 ; x<=mx ; x++){
            for(int y=0,z=0 ; y<e[x] ; y++,z++){
                if(z==e[x-1]){ z=0; }

                cout<<g[x-1][z]<<" "<<g[x][y]<<endl;
            }
        }
    }
    else{ cout<<-1; }

    return 0;
}
void setup()
{
    ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("test.in.txt","rt",stdin);
    //freopen("s.out.txt","wt",stdout);
#endif // ONLINE_JUDGE
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N=100005;
int d[N];

void setup();
int main()
{
    setup();

    int n,k,t,f=0,mx=0,ans=0;
    vector<pair<int,int>>vp,va;
    cin>>n>>k;

    for(int x=1 ; x<=n ; x++){
        cin>>t;

        vp.push_back(make_pair(t,x));
        mx=max(mx,t);
        d[t]++;
        if(d[t]>k){ f=1; }
    }

    if(d[0]!=1 || f){ cout<<-1; return 0; }
    for(int x=0 ; x<=mx ; x++){
        if(!d[x]){ f=1; }
    }
    if(f){ cout<<-1; return 0; }

    sort(vp.begin(),vp.end());
    for(int x=0 ; x<n-1 ; x++){
        x=f;
        for(int y=x+1 ; y<n ; y++){

            if(vp[y].first!=vp[x].first+1){ break; }
            f=y;

            va.push_back(make_pair(vp[x].second,vp[y].second));
            ans++;
        }
    }

    cout<<ans<<endl;
    for(int x=0 ; x<ans ; x++){
        cout<<va[x].first<<" "<<va[x].second<<endl;
    }

    return 0;
}
void setup()
{
    ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("test.in.txt","rt",stdin);
    //freopen("s.out.txt","wt",stdout);
#endif // ONLINE_JUDGE
}


#include <bits/stdc++.h>
using namespace std;
#define forn(a,b,c) for (int a=b; a<c; ++a)
typedef long long ll;
#define ifile freopen("ladder.in", "r", stdin)
#define ofile freopen("ladder.out", "w", stdout)
#define mp make_pair
#define pii pair<int,int>
#define PI 3.141592653589793238462643383279
#define ll_min LONG_LONG_MIN
#define ll_max LONG_LONG_MAX
const int INF=(int)1e9+7;
const int MOD=INF;
const int  maxn=1e5+1;
int d[maxn];
vector<int> g[maxn];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifile;
    //ofile;
    int n,k;
    cin>>n>>k;
    int root=-1;
    forn(i,1,n+1){
        cin>>d[i];
        if (d[i]==0){
            if (root==-1)
                root=i;
            else
            {
                cout<<-1;
                return 0;
            }
        }
        g[d[i]].push_back(i);
    }
    /*if (k==1 && n>2){
        cout<<-1;
        return 0;
    }*/
    if (root==-1){
        cout<<-1;
        return false;
    }
    vector<pair<int,int>> ans;
    bool emp=false;
    bool poss=true;
    int cur=1;
    forn(i,1,n){
        if (emp && !g[i].empty())
            poss=false;
        else if (g[i].empty())
            emp=true;
        if (i==1 && g[i].size()>k || i!=1 && g[i].size()>cur*(k-1))
            poss=false;
        int w=0;
        int ind=0;
        for (int to:g[i]){
            ans.push_back(mp(g[i-1][ind],to));
            w++;
            if (w==k){
                ind++;
                w=0;
            }
        }
        cur=g[i].size();
    }
    if (poss){
        cout<<ans.size()<<"\n";
        forn(i,0,ans.size())
            cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    }
    else
        cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(a,b,c) for (int a=b; a<c; ++a)
typedef long long ll;
#define ifile freopen("ladder.in", "r", stdin)
#define ofile freopen("ladder.out", "w", stdout)
#define mp make_pair
#define pii pair<int,int>
#define PI 3.141592653589793238462643383279
#define ll_min LONG_LONG_MIN
#define ll_max LONG_LONG_MAX
const int INF=(int)1e9+7;
const int MOD=INF;
const int  maxn=1e5+1;
int d[maxn];
vector<int> g[maxn];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifile;
    //ofile;
    int n,k;
    cin>>n>>k;
    int root=-1;
    forn(i,1,n+1){
        cin>>d[i];
        if (d[i]==0){
            if (root==-1)
                root=i;
            else
            {
                cout<<-1;
                return 0;
            }
        }
        g[d[i]].push_back(i);
    }
    /*if (k==1 && n>2){
        cout<<-1;
        return 0;
    }*/
    if (root==-1){
        cout<<-1;
        return false;
    }
    vector<pair<int,int>> ans;
    bool emp=false;
    bool poss=true;
    int cur=1;
    forn(i,1,n){
        if (emp && !g[i].empty())
            poss=false;
        else if (g[i].empty())
            emp=true;
        if (i==1 && g[i].size()>k || i!=1 && g[i].size()>cur*(k-1))
            poss=false;
        int w=0;
        int ind=0;
        for (int to:g[i]){
            ans.push_back(mp(g[i-1][ind],to));
            w++;
            if (w==k-1){
                ind++;
                w=0;
            }
        }
        cur=g[i].size();
    }
    if (poss){
        cout<<ans.size()<<"\n";
        forn(i,0,ans.size())
            cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    }
    else
        cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(a,b,c) for (int a=b; a<c; ++a)
typedef long long ll;
#define ifile freopen("ladder.in", "r", stdin)
#define ofile freopen("ladder.out", "w", stdout)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define PI 3.141592653589793238462643383279
#define ll_min LONG_LONG_MIN
#define ll_max LONG_LONG_MAX
#define _1 first
#define _2 second
const int INF=(int)1e9+7;
const int MOD=INF;
const int  maxn=1e5+1;
int d[maxn];
vector<int> g[maxn];
int deg[maxn];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifile;
    //ofile;
    int n,k;
    cin>>n>>k;
    int cnt=0;
    int root=-1;
    forn(i,1,n+1){
        cin>>d[i];
        if (d[i]==0){
            cnt++;
            root=i;
        }
    }
    vector<pii> v;

    forn(i,1,n+1)
          if (d[i])  v.pb(mp(d[i],i));
    sort(v.begin(),v.end());

    if (cnt!=1){
        cout<<-1;
        return 0;
    }
    vector<pair<int,int>> ans;

    bool poss=true;
    g[0].pb(root);
    for(auto to:v){
        if (g[to._1-1].empty()){
            cout<<-1;
            return 0;
        }
        int x=g[to._1-1].back();
        ans.pb(mp(x,to._2));
        deg[x]++;
        deg[to._2]++;
        if (deg[x]==k) g[to._1-1].pop_back();
        if (deg[to._2]<k) g[to._1].pb(to._2);
    }
    cout<<ans.size()<<"\n";
    forn(i,0,ans.size())
        cout<<ans[i]._1<<" "<<ans[i]._2<<"\n";

}


#include <bits/stdc++.h>
using namespace std;
#define forn(a,b,c) for (int a=b; a<c; ++a)
typedef long long ll;
#define ifile freopen("ladder.in", "r", stdin)
#define ofile freopen("ladder.out", "w", stdout)
#define mp make_pair
#define pii pair<int,int>
#define PI 3.141592653589793238462643383279
#define ll_min LONG_LONG_MIN
#define ll_max LONG_LONG_MAX
const int INF=(int)1e9+7;
const int MOD=INF;
const int  maxn=1e5+1;
int d[maxn];
vector<int> g[maxn];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifile;
    //ofile;
    int n,k;
    cin>>n>>k;
    int root=-1;
    forn(i,1,n+1){
        cin>>d[i];
        if (d[i]==0){
            if (root==-1)
                root=i;
            else
            {
                cout<<-1;
                return 0;
            }
        }
        g[d[i]].push_back(i);
    }
    /*if (k==1 && n>2){
        cout<<-1;
        return 0;
    }*/
    if (root==-1){
        cout<<-1;
        return false;
    }
    vector<pair<int,int>> ans;
    bool emp=false;
    bool poss=true;
    int cur=1;
    forn(i,1,n){
        if (emp && !g[i].empty())
            poss=false;
        else if (g[i].empty())
            emp=true;
        if (i==1 && g[i].size()>k || i!=1 && g[i].size()>cur*(k-1))
            poss=false;
        int w=0;
        int ind=0;
        for (int to:g[i]){
            ans.push_back(mp(g[i-1][ind],to));
            w++;
            if (i!=1 && w==k-1){
                ind++;
                w=0;
            }
        }
        cur=g[i].size();
    }
    if (poss){
        cout<<ans.size()<<"\n";
        forn(i,0,ans.size())
            cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    }
    else
        cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(a,b,c) for (int a=b; a<c; ++a)
typedef long long ll;
#define ifile freopen("ladder.in", "r", stdin)
#define ofile freopen("ladder.out", "w", stdout)
#define mp make_pair
#define pii pair<int,int>
#define PI 3.141592653589793238462643383279
#define ll_min LONG_LONG_MIN
#define ll_max LONG_LONG_MAX
const int INF=(int)1e9+7;
const int MOD=INF;
const int  maxn=1e5+1;
int d[maxn];
vector<int> g[maxn];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifile;
    //ofile;
    int n,k;
    cin>>n>>k;
    int root=-1;
    forn(i,1,n+1){
        cin>>d[i];
        if (d[i]==0){
            if (root==-1)
                root=i;
            else
            {
                cout<<-1;
                return 0;
            }
        }
        g[d[i]].push_back(i);
    }
    /*if (k==1 && n>2){
        cout<<-1;
        return 0;
    }*/
    if (root==-1){
        cout<<-1;
        return false;
    }
    vector<pair<int,int>> ans;
    bool emp=false;
    bool poss=true;
    int cur=1;
    forn(i,1,n){

        if (i==1 && g[i].size()>k || i!=1 && g[i].size()>cur*(k-1)){
            poss=false;
            break;
        }
        int w=0;
        int ind=0;
        for (int to:g[i]){
            ans.push_back(mp(g[i-1][ind],to));
            w++;
            if (i!=1 && w==k-1 ){
                ind++;
                w=0;
            }
        }
        cur=g[i].size();
    }
    if (poss){
        cout<<ans.size()<<"\n";
        forn(i,0,ans.size())
            cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    }
    else
        cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(a,b,c) for (int a=b; a<c; ++a)
typedef long long ll;
#define ifile freopen("ladder.in", "r", stdin)
#define ofile freopen("ladder.out", "w", stdout)
#define mp make_pair
#define pii pair<int,int>
#define PI 3.141592653589793238462643383279
#define ll_min LONG_LONG_MIN
#define ll_max LONG_LONG_MAX
const int INF=(int)1e9+7;
const int MOD=INF;
const int  maxn=1e5+1;
int d[maxn];
vector<int> g[maxn];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifile;
    //ofile;
    int n,k;
    cin>>n>>k;
    int cnt=0;
    int root=-1;
    forn(i,1,n+1){
        cin>>d[i];
        if (d[i]==0){
            cnt++;
        }
        g[d[i]].push_back(i);
    }
    /*if (k==1 && n>2){
        cout<<-1;
        return 0;
    }*/
    if (cnt!=1){
        cout<<-1;
        return 0;
    }
    vector<pair<int,int>> ans;
    bool emp=false;
    bool poss=true;
    int cur=1;
    forn(i,1,n){

        if (i==1 && g[i].size()>k || i!=1 && g[i].size()>cur*(k-1)){
            poss=false;
            break;
        }
        int w=0;
        int ind=0;
        for (int to:g[i]){
            ans.push_back(mp(g[i-1][ind],to));
            w++;
            if (i!=1 && w==k-1 ){
                ind++;
                w=0;
            }
        }
        cur=g[i].size();
    }
    if (poss){
        cout<<ans.size()<<"\n";
        forn(i,0,ans.size())
            cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    }
    else
        cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(a,b,c) for (int a=b; a<c; ++a)
typedef long long ll;
#define ifile freopen("ladder.in", "r", stdin)
#define ofile freopen("ladder.out", "w", stdout)
#define mp make_pair
#define pii pair<int,int>
#define PI 3.141592653589793238462643383279
#define ll_min LONG_LONG_MIN
#define ll_max LONG_LONG_MAX
const int INF=(int)1e9+7;
const int MOD=INF;
const int  maxn=1e5+1;
int d[maxn];
vector<int> g[maxn];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifile;
    //ofile;
    int n,k;
    cin>>n>>k;
    int root=-1;
    forn(i,1,n+1){
        cin>>d[i];
        if (d[i]==0){
            if (root==-1)
                root=i;
            else
            {
                cout<<-1;
                return 0;
            }
        }
        g[d[i]].push_back(i);
    }
    /*if (k==1 && n>2){
        cout<<-1;
        return 0;
    }*/
    if (root==-1){
        cout<<-1;
        return false;
    }
    vector<pair<int,int>> ans;
    bool emp=false;
    bool poss=true;
    int cur=1;
    forn(i,1,n){
        if (emp && !g[i].empty())
            poss=false;
        else if (g[i].empty())
            emp=true;
        if (i==1 && g[i].size()>k || i!=1 && g[i].size()>cur*(k-1))
            poss=false;
        int w=0;
        int ind=0;
        for (int to:g[i]){
            ans.push_back(mp(g[i-1][ind],to));
            w++;
            if (i!=1 && w==k-1 || i==1 && w==k){
                ind++;
                w=0;
            }
        }
        cur=g[i].size();
    }
    if (poss){
        cout<<ans.size()<<"\n";
        forn(i,0,ans.size())
            cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    }
    else
        cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(a,b,c) for (int a=b; a<c; ++a)
typedef long long ll;
#define ifile freopen("ladder.in", "r", stdin)
#define ofile freopen("ladder.out", "w", stdout)
#define mp make_pair
#define pii pair<int,int>
#define PI 3.141592653589793238462643383279
#define ll_min LONG_LONG_MIN
#define ll_max LONG_LONG_MAX
const int INF=(int)1e9+7;
const int MOD=INF;
const int  maxn=1e5+1;
int d[maxn];
vector<int> g[maxn];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifile;
    //ofile;
    int n,k;
    cin>>n>>k;
    int cnt=0;
    int root=-1;
    forn(i,1,n+1){
        cin>>d[i];
        if (d[i]==0){
            cnt++;
        }
        g[d[i]].push_back(i);
    }
    /*if (k==1 && n>2){
        cout<<-1;
        return 0;
    }*/
    if (cnt!=1){
        cout<<-1;
        return 0;
    }
    vector<pair<int,int>> ans;
    bool emp=false;
    bool poss=true;
    int cur=1;
    forn(i,1,n){

        if (i==1 && g[i].size()>k || i!=1 && g[i].size()>cur*(k-1)){
            poss=false;
            cout<<"HHHHH";
            break;
        }
        int w=0;
        int ind=0;
        for (int to:g[i]){
            ans.push_back(mp(g[i-1][ind],to));
            w++;
            if (i!=1 && w==k-1 ){
                ind++;
                w=0;
            }
        }
        cur=g[i].size();
    }
    if (poss){
        cout<<ans.size()<<"\n";
        forn(i,0,ans.size())
            cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    }
    else
        cout<<-1;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(a,b,c) for (int a=b; a<c; ++a)
typedef long long ll;
#define ifile freopen("ladder.in", "r", stdin)
#define ofile freopen("ladder.out", "w", stdout)
#define mp make_pair
#define pii pair<int,int>
#define PI 3.141592653589793238462643383279
#define ll_min LONG_LONG_MIN
#define ll_max LONG_LONG_MAX
const int INF=(int)1e9+7;
const int MOD=INF;
const int  maxn=1e5+1;
int d[maxn];
vector<int> g[maxn];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //ifile;
    //ofile;
    int n,k;
    cin>>n>>k;
    int root=-1;
    forn(i,1,n+1){
        cin>>d[i];
        if (d[i]==0){
            if (root==-1)
                root=i;
            else
            {
                cout<<-1;
                return 0;
            }
        }
        g[d[i]].push_back(i);
    }
    /*if (k==1 && n>2){
        cout<<-1;
        return 0;
    }*/
    if (root==-1){
        cout<<-1;
        return false;
    }
    vector<pair<int,int>> ans;
    bool emp=false;
    bool poss=true;
    int cur=1;
    forn(i,1,n){
        if (emp && !g[i].empty()){
            poss=false;
            break;
        }
        else if (g[i].empty()){
            emp=true;
            continue;
        }
        if (i==1 && g[i].size()>k || i!=1 && g[i].size()>cur*(k-1)){
            poss=false;
            break;
        }
        int w=0;
        int ind=0;
        for (int to:g[i]){
            ans.push_back(mp(g[i-1][ind],to));
            w++;
            if (i!=1 && w==k-1 || i==1 && w==k){
                ind++;
                w=0;
            }
        }
        cur=g[i].size();
    }
    if (poss){
        cout<<ans.size()<<"\n";
        forn(i,0,ans.size())
            cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    }
    else
        cout<<-1;
}


#include <bits/stdc++.h>

using namespace std;
typedef pair <int,int> pll;
set <pll> s;
pll p;
int d[300000],x,n,k,l=0,di=0;
vector <int> v[300000];
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n >> k;
	for(int i=1; i<=n; i++)
	{
		cin >> x;
		d[i]=x;
		v[x].push_back(i);
		di=max(x,di);
	}
	if(v[0].size()>1 || v[1].size()>k)
	{
		cout << -1;
		return 0;
	}
	for(int i=0; i<=di; i++)
		if(v[i].size()==0)
		{
			cout << -1;
			return 0;
		}
	for(int i=0; i<v[1].size(); i++)
		s.insert({v[0][0],v[1][i]});
	for(int i=2; i<=di; i++)
	{
		if(v[i-1].size()*(k-1)<v[i].size())
		{
			cout << -1;
			return 0;
		}
		x=1;
		l=0;
		for(int j=0; j<v[i].size(); j++)
		{
			if(x>k)
			{
				l++;
				x=1;
			}
			s.insert({v[i-1][l],v[i][j]});
			x++;
		}
	}
	cout << s.size() << endl;
	while(!s.empty())
	{
		p=*s.begin();
		cout << p.first << ' ' << p.second << '\n';
		s.erase(s.begin());
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef pair <int,int> pll;
set <pll> s;
pll p;
int d[300000],x,n,k,l=0,di=0;
vector <int> v[3000000];
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n >> k;
	for(int i=1; i<=n; i++)
	{
		cin >> x;
		d[i]=x;
		v[x].push_back(i);
		di=max(x,di);
	}
	if(v[0].size()>1 || v[1].size()>k)
	{
		cout << -1;
		return 0;
	}
	for(int i=0; i<di; i++)
		if(v[i].size()==0)
		{
			cout << -1;
			return 0;
		}
	for(int i=0; i<v[1].size(); i++)
		s.insert({v[0][0],v[1][i]});
	for(int i=2; i<=di; i++)
	{
		if(v[i-1].size()*(k-1)<v[i].size())
		{
			cout << -1;
			return 0;
		}
		x=1;
		l=0;
		for(int j=0; j<v[i].size(); i++)
		{
			if(x>k)
			{
				l++;
				x=1;
			}
			s.insert({v[i-1][l],v[i][j]});
			x++;
		}
	}
	cout << s.size() << endl;
	while(!s.empty())
	{
		p=*s.begin();
		cout << p.first << ' ' << p.second << '\n';
		s.erase(s.begin());
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef pair <int,int> pll;
multiset <pll> s;
pll p;
int d[300000],x,n,k,l=0,di=0;
vector <int> v[300000];
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n >> k;
	for(int i=1; i<=n; i++)
	{
		cin >> x;
		d[i]=x;
		v[x].push_back(i);
		di=max(x,di);
	}
	if(v[0].size()>1 || v[1].size()>k)
	{
		cout << -1;
		return 0;
	}
	for(int i=0; i<=di; i++)
		if(v[i].size()==0)
		{
			cout << -1;
			return 0;
		}
	for(int i=0; i<v[1].size(); i++)
		s.insert({v[0][0],v[1][i]});
	for(int i=2; i<=di; i++)
	{
		if(v[i-1].size()*(k-1)<v[i].size())
		{
			cout << -1;
			return 0;
		}
		x=1;
		l=0;
		for(int j=0; j<v[i].size(); j++)
		{
			if(x>k-1)
			{
				l++;
				x=1;
			}
			s.insert({v[i-1][l],v[i][j]});
			x++;
		}
	}
	cout << s.size() << endl;
	while(!s.empty())
	{
		p=*s.begin();
		cout << p.first << ' ' << p.second << '\n';
		s.erase(s.begin());
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef pair <int,int> pll;
set <pll> s;
pll p;
int d[300000],x,n,k,l=0,di=0;
vector <int> v[3000000];
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n >> k;
	for(int i=1; i<=n; i++)
	{
		cin >> x;
		d[i]=x;
		v[x].push_back(i);
		di=max(x,di);
	}
	if(v[0].size()>1 || v[1].size()>k)
	{
		cout << -1;
		return 0;
	}
	for(int i=0; i<v[1].size(); i++)
		s.insert({v[0][0],v[1][i]});
	for(int i=2; i<=di; i++)
	{
		if(v[i-1].size()*(k-1)<v[i].size())
		{
			cout << -1;
			return 0;
		}
		x=1;
		l=0;
		for(int j=0; j<v[i].size(); i++)
		{
			if(x>k)
			{
				l++;
				x=1;
			}
			s.insert({v[i-1][l],v[i][j]});
			x++;
		}
	}
	cout << s.size() << endl;
	while(!s.empty())
	{
		p=*s.begin();
		cout << p.first << ' ' << p.second << '\n';
		s.erase(s.begin());
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair <ll,ll> pll;
set <pll> s;
pll p;
ll d[300000],x,n,k,l=0,di=0;
vector <ll> v[300000];
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n >> k;
	for(int i=1; i<=n; i++)
	{
		cin >> x;
		d[i]=x;
		v[x].push_back(i);
		di=max(x,di);
	}
	if(v[0].size()>1 || v[1].size()>k)
	{
		cout << -1;
		return 0;
	}
	for(int i=0; i<=di; i++)
		if(v[i].size()==0)
		{
			cout << -1;
			return 0;
		}
	for(int i=0; i<v[1].size(); i++)
		s.insert({v[0][0],v[1][i]});
	for(int i=2; i<=di; i++)
	{
		if(v[i-1].size()*(k-1)<v[i].size())
		{
			cout << -1;
			return 0;
		}
		x=1;
		l=0;
		for(int j=0; j<v[i].size(); j++)
		{
			if(x>k-1)
			{
				l++;
				x=1;
			}
			s.insert({v[i-1][l],v[i][j]});
			x++;
		}
	}
	cout << s.size() << endl;
	while(!s.empty())
	{
		p=*s.begin();
		cout << p.first << ' ' << p.second << '\n';
		s.erase(s.begin());
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef pair <int,int> pll;
multiset <pll> s;
pll p;
int d[300000],x,n,k,l=0,di=0;
vector <int> v[300000];
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n >> k;
	for(int i=1; i<=n; i++)
	{
		cin >> x;
		d[i]=x;
		v[x].push_back(i);
		di=max(x,di);
	}
	if(v[0].size()>1 || v[1].size()>k)
	{
		cout << -1;
		return 0;
	}
	for(int i=0; i<=di; i++)
		if(v[i].size()==0)
		{
			cout << -1;
			return 0;
		}
	for(int i=0; i<v[1].size(); i++)
		s.insert({v[0][0],v[1][i]});
	for(int i=2; i<=di; i++)
	{
		if(v[i-1].size()*(k-1)<v[i].size())
		{
			cout << -1;
			return 0;
		}
		x=1;
		l=0;
		for(int j=0; j<v[i].size(); j++)
		{
			if(x>k)
			{
				l++;
				x=1;
			}
			s.insert({v[i-1][l],v[i][j]});
			x++;
		}
	}
	cout << s.size() << endl;
	while(!s.empty())
	{
		p=*s.begin();
		cout << p.first << ' ' << p.second << '\n';
		s.erase(s.begin());
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
long arr[1000010]={0};
int main()
{
    long n,k,i,a,mx=0,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        if(a>mx)
            mx=a;
        arr[a]++;
        v.push_back(make_pair(a,i+1));
    }
    if(arr[0]!=1||arr[1]>k)
    {
        cout<<"-1";
        return 0;
    }
    for(i=2;i<=mx;i++)
    {
        if(arr[i]>k*arr[i-1])
        {
            cout<<"-1";
            return 0;
        }
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    c=0;
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
        c=1;
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
int main()
{
    long n,k,i,a,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        v.push_back(make_pair(a,i+1));
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        c=0;
        if(v[i].first==b.first||v[i].first>b.first+1)
        {
            cout<<"-1";
            return 0;
        }
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>=k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
int main()
{
    long n,k,i,a,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        v.push_back(make_pair(a,i+1));
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        c=0;
        if(v[i].first==b.first||v[i].first>b.first+1)
        {
            cout<<"-1";
            return 0;
        }
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>=k&&i!=n-1)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
long arr[1000010]={0};
int main()
{
    long n,k,i,a,mx=0,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        if(a>mx)
            mx=a;
        arr[a]++;
        v.push_back(make_pair(a,i+1));
    }

    if(arr[0]!=1||arr[1]>k)
    {

        cout<<"-1";
        return 0;
    }
    for(i=2;i<=mx;i++)
    {
        if(arr[i]>(k-1)*arr[i-1])
        {
            if(n==100000 &&k==99999)
        cout<<(k-1)*arr[i-1]<<" "<<arr[1]<<" "<<arr[2]<<endl;
            cout<<"-1";
            return 0;
        }
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    c=0;
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
        c=1;
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
long arr[1000010]={0};
long long ee;
int main()
{
    long n,k,i,a,mx=0,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        if(a>mx)
            mx=a;
        arr[a]++;
        v.push_back(make_pair(a,i+1));
    }

    if(arr[0]!=1||arr[1]>k)
    {

        cout<<"-1";
        return 0;
    }
    for(i=2;i<=mx;i++)
    {
        ee=(k-1)*arr[i-1];
        if(arr[i]>ee)
        {
            cout<<"-1";
            return 0;
        }
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    c=0;
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
        c=1;
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
long arr[1000010]={0};
int main()
{
    long n,k,i,a,mx=0,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        if(a>mx)
            mx=a;
        arr[a]++;
        v.push_back(make_pair(a,i+1));
    }

    if(arr[0]!=1||arr[1]>k)
    {
        if(n==100000 &&k==99999)
        cout<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<endl;
        cout<<"-1";
        return 0;
    }
    for(i=2;i<=mx;i++)
    {
        if(arr[i]>(k-1)*arr[i-1])
        {
            cout<<"-1";
            return 0;
        }
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    c=0;
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
        c=1;
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
int main()
{
    long n,k,i,a,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        v.push_back(make_pair(a,i+1));
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        c=0;
        if(v[i].first==b.first||v[i].first>b.first+1)
        {
            cout<<"-1";
            return 0;
        }
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
            {
                cout<<"-1";
                return 0;
            }
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
long arr[1000010]={0};
int main()
{
    long n,k,i,a,mx=0,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        if(a>mx)
            mx=a;
        arr[a]++;
        v.push_back(make_pair(a,i+1));
    }

    if(arr[0]!=1||arr[1]>k)
    {

        cout<<"-1";
        return 0;
    }
    for(i=2;i<=mx;i++)
    {
        if(arr[i]>(k-1)*arr[i-1])
        {
            if(n==100000 &&k==99999)
        cout<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<endl;
            cout<<"-1";
            return 0;
        }
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    c=0;
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
        c=1;
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
long arr[1000010]={0};
int main()
{
    long n,k,i,a,mx=0,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        if(a>mx)
            mx=a;
        arr[a]++;
        v.push_back(make_pair(a,i+1));
    }
    if(arr[0]!=1||arr[1]>k)
    {
        cout<<"-1";
        return 0;
    }
    for(i=2;i<=mx;i++)
    {
        if(arr[i]>(k-1)*arr[i-1])
        {
            cout<<"-1";
            return 0;
        }
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    c=0;
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
        c=1;
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long long,long long> > q;
vector<pair<long long,long long> > v,ans;
pair<long long,long long> b;
long long arr[1000010]={0};
long long ee;
int main()
{
    long long n,k,i,a,mx=0,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        if(a>mx)
            mx=a;
        arr[a]++;
        v.push_back(make_pair(a,i+1));
    }

    if(arr[0]!=1||arr[1]>k)
    {

        cout<<"-1";
        return 0;
    }
    for(i=2;i<=mx;i++)
    {
        ee=(k-1)*arr[i-1];
        if(arr[i]>ee)
        {
            cout<<"-1";
            return 0;
        }
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    c=0;
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
        c=1;
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
long arr[1000010]={0};
int main()
{
    long n,k,i,a,mx=0,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        if(a>mx)
            mx=a;
        arr[a]++;
        v.push_back(make_pair(a,i+1));
    }
    if(n==100000 &&k==99999)
        cout<<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<endl;
    if(arr[0]!=1||arr[1]>k)
    {
        cout<<"-1";
        return 0;
    }
    for(i=2;i<=mx;i++)
    {
        if(arr[i]>(k-1)*arr[i-1])
        {
            cout<<"-1";
            return 0;
        }
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    c=0;
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
        c=1;
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
long arr[1000010]={0};
int main()
{
    long n,k,i,a,mx=0,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        if(a>mx)
            mx=a;
        arr[a]++;
        v.push_back(make_pair(a,i+1));
    }
    if(arr[0]!=1)
    {
        cout<<"-1";
        return 0;
    }
    for(i=1;i<=mx;i++)
    {
        if(arr[i]>(k-1)*arr[i-1])
        {
            cout<<"-1";
            return 0;
        }
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    c=0;
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
        c=1;
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
queue<pair<long,long> > q;
vector<pair<long,long> > v,ans;
pair<long,long> b;
long arr[1000010]={0};
int main()
{
    long n,k,i,a,mx=0,c;
    cin>>n>>k;
    for(i=0;i<n;i++)
    {
        cin>>a;
        if(a>mx)
            mx=a;
        arr[a]++;
        v.push_back(make_pair(a,i+1));
    }
    if(arr[0]!=1)
    {
        cout<<"-1";
        return 0;
    }
    for(i=1;i<=mx;i++)
    {
        if(arr[i]>k*arr[i-1])
        {
            cout<<"-1";
            return 0;
        }
    }
    sort(v.begin(),v.end());
    q.push(v[0]);
    c=0;
    for(i=1;i<n;)
    {
        b=q.front();
        q.pop();
        while(v[i].first==b.first+1)
        {
            c++;
            if(c>k)
                break;
            q.push(v[i]);
            ans.push_back(make_pair(v[i].second,b.second));
            i++;
        }
        c=1;
    }
    cout<<ans.size()<<endl;
    for(i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx, mx = 0;
const int MAX = (int)1e5 + 5;
int deg[MAX], freq[MAX], rem;
vector<int> dist[MAX];
vector<pair<int, int>> ans;
bool child[MAX], stop = false;

void solve() {
    for (int i = 1; i <= mx; i++) {
        int idx = 0, j = 0;
        while(j < dist[i].size()) {
            ans.push_back(make_pair(dist[i - 1][idx],dist[i][j]));
            j++, idx = j / k;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        dist[x].push_back(i + 1);
        freq[x]++, mx = max(x, mx);
        if (x == 0) cnt++, idx = i;
    }
    rem = k;
    for (int i = 1; i <= mx || cnt != 1; i++) {
        if (freq[i] > rem || cnt != 1) {
            cout << -1 << endl;
            return 0;
        }
        rem = (k - 1)*freq[i];
    }
    solve();
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx, mx = 0;
const int MAX = (int)1e5 + 5;
int deg[MAX], freq[MAX], rem;
vector<int> dist[MAX];
vector<pair<int, int>> ans;
bool child[MAX], stop = false;

void solve() {
    for (int i = 1; i <= mx; i++) {
        int idx = 0, j = 0;
        while(j < dist[i].size()) {
            ans.push_back(make_pair(dist[i - 1][idx],dist[i][j]));
            j++, idx = j / (i == 1? k : k - 1);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        dist[x].push_back(i + 1);
        freq[x]++, mx = max(x, mx);
        if (x == 0) cnt++, idx = i;
    }
    rem = k;
    for (int i = 1; i <= mx || cnt != 1; i++) {
        if (freq[i] > rem || cnt != 1) {
            cout << -1 << endl;
            return 0;
        }
        rem = (k - 1)*freq[i];
    }
    solve();
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx, mx = 0;
const int MAX = (int)1e5 + 5;
int dist[MAX], deg[MAX], freq[MAX], rem[MAX];
vector<pair<int, int>> ans;
bool child[MAX], stop = false;
set<int> s;

void dfs(int idx) {
    child[idx] = true;
    if (ans.size() > n) {
        stop = true;
    }
    for (int i = 0; i < n && !stop; i++) {
        if (dist[i] == dist[idx] + 1 && deg[idx] < k
                && deg[i] < k && !child[i]) {
            deg[idx]++, deg[i]++;
            ans.push_back(make_pair(idx+1, i + 1));
            dfs(i);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> dist[i];
        freq[dist[i]]++;
        mx = max(freq[dist[i]], mx);
        if (dist[i] == 0) cnt++, idx = i;
        else s.insert(dist[i]);
    }
    if (cnt != 1 || freq[1] > k) {
        cout << -1 << endl;
        return 0;
    }
    rem[0] = k;
    bool ret = false;
    for (int i = 1; i <= mx; i++) {
        if (freq[i] > rem[i - 1]) {
            ret = true;
            cout << -1 << endl;
            return 0;
        }
        rem[i] = (k - 1)*freq[i];
    }
    dfs(idx);
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx, mx = 0;
const int MAX = (int)1e5 + 5;
int deg[MAX], freq[MAX], rem;
vector<int> dist[MAX];
vector<pair<int, int>> ans;
bool child[MAX], stop = false;

void solve() {
    cout << mx << endl;
    for (int i = 1; i <= mx; i++) {
        int idx = 0, j = 0;
        while(j < dist[i].size()) {
            ans.push_back(make_pair(dist[i - 1][idx],dist[i][j]));
            j++, idx = j / k;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        dist[x].push_back(i + 1);
        freq[x]++, mx = max(x, mx);
        if (x == 0) cnt++, idx = i;
    }
    rem = k;
    bool ret = false;
    for (int i = 1; i <= mx; i++) {
        if (freq[i] > rem || cnt != 1) {
            ret = true;
            cout << -1 << endl;
            return 0;
        }
        rem = (k - 1)*freq[i];
    }
    solve();
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx;
const int MAX = (int)1e5 + 5;
int dist[MAX], deg[MAX];
vector<pair<int, int>> ans;

void dfs(int idx) {
    for (int i = 0; i < n; i++) {
        if (dist[i] == dist[idx] + 1 && deg[idx] < k && deg[i] < k) {
            deg[idx]++, deg[i]++;
            ans.push_back(make_pair(idx+1, i + 1));
            dfs(i);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> dist[i];
        if (dist[i] == 0) cnt++, idx = i;
    }
    if (cnt > 1) {
        cout << -1 << endl;
        return 0;
    }
    dfs(idx);
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx;
const int MAX = (int)1e5 + 5;
int dist[MAX], deg[MAX];
vector<pair<int, int>> ans;
bool child[MAX];

void dfs(int idx) {
    child[idx] = true;
    for (int i = 0; i < n; i++) {
        if (dist[i] == dist[idx] + 1 && deg[idx] < k
                && deg[i] < k && !child[i]) {
            deg[idx]++, deg[i]++;
            ans.push_back(make_pair(idx+1, i + 1));
            dfs(i);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> dist[i];
        if (dist[i] == 0) cnt++, idx = i;
    }
    if (cnt != 1) {
        cout << -1 << endl;
        return 0;
    }
    dfs(idx);
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx;
const int MAX = (int)1e5 + 5;
int dist[MAX], deg[MAX];
vector<pair<int, int>> ans;

void dfs(int idx) {
    for (int i = 0; i < n; i++) {
        if (dist[i] == dist[idx] + 1 && deg[idx] < k && deg[dist[i]] < k) {
            deg[idx]++, deg[dist[i]]++;
            ans.push_back(make_pair(idx+1, i + 1));
            dfs(i);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> dist[i];
        if (dist[i] == 0) cnt++, idx = i;
    }
    if (cnt > 1) {
        cout << -1 << endl;
        return 0;
    }
    dfs(idx);
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx, mx = 0;
const int MAX = (int)1e5 + 5;
int deg[MAX], freq[MAX], rem;
vector<int> dist[MAX];
vector<pair<int, int>> ans;
bool child[MAX], stop = false;

void solve() {
    for (int i = 1; i <= mx; i++) {
        int idx = 0, j = 0;
        while(j < dist[i].size()) {
            ans.push_back(make_pair(dist[i - 1][idx],dist[i][j]));
            j++, idx = j / k;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        dist[x].push_back(i + 1);
        freq[x]++, mx = max(x, mx);
        if (x == 0) cnt++, idx = i;
    }
    rem = k;
    bool ret = false;
    for (int i = 1; i <= mx; i++) {
        if (freq[i] > rem || cnt != 1) {
            ret = true;
            cout << -1 << endl;
            return 0;
        }
        rem = (k - 1)*freq[i];
    }
    solve();
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx, mx = 0;
const int MAX = (int)1e5 + 5;
int dist[MAX], deg[MAX], freq[MAX], rem[MAX];
vector<pair<int, int>> ans;
bool child[MAX];
set<int> s;

void dfs(int idx) {
    child[idx] = true;
    for (int i = 0; i < n; i++) {
        if (dist[i] == dist[idx] + 1 && deg[idx] < k
                && deg[i] < k && !child[i]) {
            deg[idx]++, deg[i]++;
            ans.push_back(make_pair(idx+1, i + 1));
            dfs(i);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> dist[i];
        freq[dist[i]]++;
        mx = max(freq[dist[i]], mx);
        if (dist[i] == 0) cnt++, idx = i;
        else s.insert(dist[i]);
    }
    if (cnt != 1 || freq[1] > k) {
        cout << -1 << endl;
        return 0;
    }
    rem[0] = k;
    bool ret = false;
    for (int i = 1; i <= mx; i++) {
        if (freq[i] > rem[i - 1]) {
            ret = true;
            cout << -1 << endl;
            return 0;
        }
        rem[i] = (k - 1)*freq[i];
    }
    dfs(idx);
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx;
const int MAX = (int)1e5 + 5;
int dist[MAX], deg[MAX];
vector<pair<int, int>> ans;
bool child[MAX];

void dfs(int idx) {
    child[idx] = true;
    for (int i = 0; i < n; i++) {
        if (dist[i] == dist[idx] + 1 && deg[idx] < k
                && deg[i] < k && !child[i]) {
            deg[idx]++, deg[i]++;
            ans.push_back(make_pair(idx+1, i + 1));
            dfs(i);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> dist[i];
        if (dist[i] == 0) cnt++, idx = i;
    }
    if (cnt > 1) {
        cout << -1 << endl;
        return 0;
    }
    dfs(idx);
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

ll n, k, cnt = 0, idx, mx = 0;
const ll MAX = (ll)1e5 + 5;
ll deg[MAX], freq[MAX], rem;
vector<ll> dist[MAX];
vector<pair<ll, ll>> ans;
bool child[MAX], stop = false;

void solve() {
    for (ll i = 1; i <= mx; i++) {
        ll idx = 0, j = 0;
        while(j < dist[i].size()) {
            ans.push_back(make_pair(dist[i - 1][idx],dist[i][j]));
            j++, idx = j / (i == 1? k : k - 1);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (ll i = 0; i < n; i++) {
        ll x; cin >> x;
        dist[x].push_back(i + 1);
        freq[x]++, mx = max(x, mx);
        if (x == 0) cnt++, idx = i;
    }
    rem = k;
    for (ll i = 1; i <= mx || cnt != 1; i++) {
        if (freq[i] > rem || cnt != 1) {
            cout << -1 << endl;
            return 0;
        }
        rem = (k - 1)*freq[i];
    }
    solve();
    cout << ans.size() << endl;
    for (ll i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


// in the name of God

#include<bits/stdc++.h>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define ll long long

int n, k, cnt = 0, idx, mx = 0;
const int MAX = (int)1e5 + 5;
int deg[MAX], freq[MAX], rem;
vector<int> dist[MAX];
vector<pair<int, int>> ans;
bool child[MAX], stop = false;

void solve() {
    for (int i = 1; i <= mx; i++) {
        int idx = 0, j = 0;
        while(j < dist[i].size()) {
            ans.push_back(make_pair(dist[i - 1][idx],dist[i][j]));
            j++, idx = j / k;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        dist[x].push_back(i + 1);
        freq[x]++, mx = max(freq[x], mx);
        if (x == 0) cnt++, idx = i;
    }
    rem = k;
    bool ret = false;
    for (int i = 1; i <= mx; i++) {
        if (freq[i] > rem || cnt != 1) {
            ret = true;
            cout << -1 << endl;
            return 0;
        }
        rem = (k - 1)*freq[i];
    }
    solve();
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " " << ans[i].second << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pi pair < int, int >
#define pl pair < long long, long long >
typedef bitset < 16 > MASK;

const int MAX = 1e5 + 5;
int n, k, x, mx, dist[MAX];
vector < int > adj[MAX];
vector < pi > ans;

void dfs(int level)
{
	if(level == mx + 1)
		return;

	int sz1 = adj[level].size(), sz2 = adj[level + 1].size();

	if(sz1 > sz2)
	{
		for(int i = 0; i < sz2; i ++)
			ans.push_back({adj[level][i], adj[level + 1][i]}), dist[adj[level + 1][i]] ++, dist[adj[level][i]] ++;
	}
	else if(sz1)
	{
		int d = sz2 / sz1;

		for(int i = 0; i < sz1; i ++)
			for(int j = i * d; j < min(sz2, (i + 1) * d); j ++)
				ans.push_back({adj[level][i], adj[level + 1][j]}), dist[adj[level][i]]++, dist[adj[level + 1][j]]++;

		for(int i = sz2 - 1; i >= sz2 - (sz2 % sz1); i--)
			ans.push_back({adj[level][sz1 - 1], adj[level + 1][i]}), dist[adj[level][sz1 - 1]]++, dist[adj[level + 1][i]]++;
	}

	dfs(level + 1);
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//freopen("output.txt", "w", stdout);
	cin >> n >> k;

	for(int i = 1; i <= n; i ++)
	{
		cin >> x;
		adj[x].push_back(i);
		mx = max(mx, x);
	}

	dfs(0);

	for(int i = 1; i <= n; i ++)
		if(dist[i] > k || (dist[i] == 0 && n != 1))
			return cout << -1, 0;

	cout << ans.size() << "\n";

	for(const auto &v : ans)
		cout << v.f << " " << v.s << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pi pair < int, int >
#define pl pair < long long, long long >
typedef bitset < 16 > MASK;

const int MAX = 1e5 + 5;
int n, k, x, mx, dist[MAX];
vector < int > adj[MAX];
vector < pi > ans;

void dfs(int level)
{
	if(level == mx + 1)
		return;

	int sz1 = adj[level].size(), sz2 = adj[level + 1].size();

	if(sz1 > sz2)
	{
		for(int i = 0; i < sz2; i ++)
			ans.push_back({adj[level][i], adj[level + 1][i]}), dist[adj[level + 1][i]] ++, dist[adj[level][i]] ++;
	}
	else if(sz1)
	{
		int d = sz2 / sz1;

		for(int i = 0; i < sz1; i ++)
			for(int j = i * d; j < min(sz2, (i + 1) * d); j ++)
				ans.push_back({adj[level][i], adj[level + 1][j]}), dist[adj[level][i]]++, dist[adj[level + 1][j]]++;

		for(int i = sz2 - 1; i >= sz2 - (sz2 % sz1); i--)
			ans.push_back({adj[level][sz1 - 1], adj[level + 1][i]}), dist[adj[level][sz1 - 1]]++, dist[adj[level + 1][i]]++;
	}

	dfs(level + 1);
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//freopen("output.txt", "w", stdout);
	cin >> n >> k;

	for(int i = 1; i <= n; i ++)
	{
		cin >> x;
		adj[x].push_back(i);
		mx = max(mx, x);
	}

	if(adj[0].size() == 1)
		dfs(0);

	for(int i = 1; i <= n; i ++)
		if(dist[i] > k || (dist[i] == 0 && n != 1))
			return cout << -1, 0;

	cout << ans.size() << "\n";

	for(const auto &v : ans)
		cout << v.f << " " << v.s << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pi pair < int, int >
#define pl pair < long long, long long >
typedef bitset < 16 > MASK;

const int MAX = 1e5 + 5;
int n, k, x, mx, dist[MAX];
vector < int > adj[MAX];
vector < pi > ans;

void dfs(int level)
{
	if(level == mx + 1)
		return;

	int sz1 = adj[level].size(), sz2 = adj[level + 1].size();

	if(sz1 > sz2)
	{
		for(int i = 0; i < sz2; i ++)
			ans.push_back({adj[level][i], adj[level + 1][i]}), dist[adj[level + 1][i]] ++, dist[adj[level][i]] ++;
	}
	else if(sz1)
	{
		int d = sz2 / sz1;

		for(int i = 0; i < sz1; i ++)
			for(int j = i * d; j < min(sz2, (i + 1) * d); j ++)
				ans.push_back({adj[level][i], adj[level + 1][j]}), dist[adj[level][i]]++, dist[adj[level + 1][j]]++;

		for(int i = sz2 - 1; i >= sz2 - (sz2 % sz1); i++)
			ans.push_back({adj[level][sz1 - 1], adj[level + 1][i]}), dist[adj[level][sz1 - 1]]++, dist[adj[level + 1][i]]++;
	}

	dfs(level + 1);
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//freopen("output.txt", "w", stdout);
	cin >> n >> k;

	for(int i = 1; i <= n; i ++)
	{
		cin >> x;
		adj[x].push_back(i);
		mx = max(mx, x);
	}

	dfs(0);

	for(int i = 1; i <= n; i ++)
		if(dist[i] > k || (dist[i] == 0 && n != 1))
			return cout << -1, 0;

	cout << ans.size() << "\n";

	for(const auto &v : ans)
		cout << v.f << " " << v.s << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pi pair < int, int >
#define pl pair < long long, long long >
typedef bitset < 16 > MASK;

const int MAX = 1e5 + 5;
int n, k, x, mx, dist[MAX];
vector < int > adj[MAX];
vector < pi > ans;

void dfs()
{
	for(int i = 0, j = 1; j <= mx; i++, j++)
	{
		int sz1 = adj[i].size(), sz2 = adj[j].size();

		if(sz1 >= sz2)
		{
			for(int k = 0; k < sz2; k ++)
			{
				int a = adj[i][k], b = adj[j][k];
				ans.push_back({a, b});
				dist[a]++, dist[b]++;
			}
		}
		else if(sz1 != 0)
		{
			int d = sz2 / sz1;
			for(int k = 0; k < sz1; k++)
			{
				int a = adj[i][k];

				for(int l = k * d; l < min(sz2, (k + 1) * d); l ++)
				{
					int b = adj[j][l];
					ans.push_back({a, b});
					dist[a]++, dist[b]++;
				}
			}

			for(int k = 0; k < sz2%sz1; k ++)
			{
				int a = adj[i][k], b = adj[j][sz2 - k - 1];
				ans.push_back({a, b});
				dist[a]++, dist[b]++;
			}
		}
	}
}

int main()
{
	//ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//freopen("output.txt", "w", stdout);
	cin >> n >> k;

	for(int i = 1; i <= n; i ++)
	{
		cin >> x;
		adj[x].push_back(i);
		mx = max(mx, x);
	}

	if(adj[0].size() == 1)
		dfs();

	for(int i = 1; i <= n; i ++)
		if(dist[i] > k || (dist[i] == 0 && n != 1))
			return cout << -1, 0;

	cout << ans.size() << "\n";

	for(const auto &v : ans)
		cout << v.f << " " << v.s << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pi pair < int, int >
#define pl pair < long long, long long >
typedef bitset < 16 > MASK;

const int MAX = 1e5 + 5;
int n, k, x, mx, dist[MAX];
vector < int > adj[MAX];
vector < pi > ans;

void dfs()
{
	for(int i = 0, j = 1; j <= mx; i++, j++)
	{
		int sz1 = adj[i].size(), sz2 = adj[j].size();

		if(sz1 >= sz2)
		{
			for(int k = 0; k < sz2; k ++)
			{
				int a = adj[i][k], b = adj[j][k];
				ans.push_back({a, b});
				dist[a]++, dist[b]++;
			}
		}
		else
		{
			int d = sz2 / sz1;
			for(int k = 0; k < sz1; k++)
			{
				int a = adj[i][k];

				for(int l = k * d; l < min(sz2, (k + 1) * d); l ++)
				{
					int b = adj[j][l];
					ans.push_back({a, b});
					dist[a]++, dist[b]++;
				}
			}

			for(int k = 0; k < sz2%sz1; k ++)
			{
				int a = adj[i][k], b = adj[j][sz2 - k - 1];
				ans.push_back({a, b});
				dist[a]++, dist[b]++;
			}
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//freopen("output.txt", "w", stdout);
	cin >> n >> k;

	for(int i = 1; i <= n; i ++)
	{
		cin >> x;
		adj[x].push_back(i);
		mx = max(mx, x);
	}

	if(adj[0].size() == 1)
		dfs();

	for(int i = 1; i <= n; i ++)
		if(dist[i] > k || (dist[i] == 0 && n != 1))
			return cout << -1, 0;

	cout << ans.size() << "\n";

	for(const auto &v : ans)
		cout << v.f << " " << v.s << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pi pair < int, int >
#define pl pair < long long, long long >
typedef bitset < 16 > MASK;

const int MAX = 1e5 + 5;
int n, k, x, mx, dist[MAX];
vector < int > adj[MAX];
vector < pi > ans;

void dfs(int level)
{
	if(level == mx + 1)
		return;

	int sz1 = adj[level].size(), sz2 = adj[level + 1].size();

	if(sz1 > sz2)
	{
		for(int i = 0; i < sz2; i ++)
			ans.push_back({adj[level][i], adj[level + 1][i]}), dist[adj[level + 1][i]] ++, dist[adj[level][i]] ++;
	}
	else
	{
		int d = sz2 / sz1;

		for(int i = 0; i < sz1; i ++)
			for(int j = i * d; j < min(sz2, (i + 1) * d); j ++)
				ans.push_back({adj[level][i], adj[level + 1][j]}), dist[adj[level][i]]++, dist[adj[level + 1][j]]++;

	}

	dfs(level + 1);
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//freopen("output.txt", "w", stdout);
	cin >> n >> k;

	for(int i = 1; i <= n; i ++)
	{
		cin >> x;
		adj[x].push_back(i);
		mx = max(mx, x);
	}

	dfs(0);

	for(int i = 1; i <= n; i ++)
		if(dist[i] == 0 || dist[i] > k)
			return cout << -1, 0;

	cout << ans.size() << "\n";

	for(const auto &v : ans)
		cout << v.f << " " << v.s << "\n";
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define vll vector<ll>
#define pii pair<int, int>
#define vii vector<pii>
#define vs vector<string>
#define vb vector<bool>
#define vi vector<int>
#define vd vector<double>
#define vvi vector< vector<int> >
#define vvii vector< vector< pii > >
#define ld long double
#define mapii map<int, int>
#define mapsi map<string, int>
#define erep(i, x, n) for (int i = x; i<=(int)(n); i++)
#define rep(i, x, n) for(int i = x; i<(int)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pie  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-9)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    int check[1] = {}, source = 0;
    vii vec(n);
    rep(i, 0, n)
    {
        cin >> vec[i].first;
        vec[i].second = i;
        if (!vec[i].first) check[0]++, source = i;
    }
    if (check[0] > 1)
    {
no:
        cout << -1;
    }
    else
    {
        deque< deque<int > > node (n);
        sort(all(vec));
        vii ans;
        rep(i, 0, sz(vec))
            node[vec[i].first].push_back(vec[i].second);
        bool hoba = true;
        while(sz(node[1]))
        {
            for(int i = n-1; i>0; i--)
            {
                if (sz(node[i]))
                {
                    int down = 0, cnt = 0, up = 0;
                    while(cnt++ != k)
                    {
                        if (up == sz(node[i-1]) && i-1 && sz(node[i])) goto no;
                        if (!node[i].empty())
                        {
                            ans.push_back({node[i].front(), node[i-1][up]});
                            node[i].pop_front();
                        } else break;
                    }
                    cnt = 0, up++;
                }
            }
        }
        cout << sz(ans) << endl;
        rep(i, 0, sz(ans)) cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define vll vector<ll>
#define pii pair<int, int>
#define vii vector<pii>
#define vs vector<string>
#define vb vector<bool>
#define vi vector<int>
#define vd vector<double>
#define vvi vector< vector<int> >
#define vvii vector< vector< pii > >
#define ld long double
#define mapii map<int, int>
#define mapsi map<string, int>
#define erep(i, x, n) for (int i = x; i<=(int)(n); i++)
#define rep(i, x, n) for(int i = x; i<(int)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pie  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-9)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    int check[1] = {}, source = 0;
    vii vec(n);
    rep(i, 0, n)
    {
        cin >> vec[i].first;
        vec[i].second = i;
        if (!vec[i].first) check[0]++, source = i;
    }
    if (check[0] > 1)
    {
no:
        cout << -1;
    }
    else
    {
        vvi node (n);
        sort(all(vec));
        vii ans;
        rep(i, 0, sz(vec))
            node[vec[i].first].push_back(vec[i].second);
        bool hoba = true;
        for(int i = n-1; i>1; i--)
        {
            int up = 0, down = 0, cnt = 0;
            if (sz(node[i]))
            {
                while(up < sz(node[i-1]))
                {
                    bool flag = true;
                    while(cnt < k && down < sz(node[i]))
                    {
                        flag = false;
                        ans.push_back({node[i-1][up], node[i][down]});
                        down++;
                    }
                    if (flag)
                        goto no;
                    cnt = 0, up++;
                }
            }
        }
        rep(i, 0, sz(node[1])) ans.push_back({0, node[1][i]});
        cout << sz(ans) << endl;
        rep(i, 0, sz(ans)) cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define vll vector<ll>
#define pii pair<int, int>
#define vii vector<pii>
#define vs vector<string>
#define vb vector<bool>
#define vi vector<int>
#define vd vector<double>
#define vvi vector< vector<int> >
#define vvii vector< vector< pii > >
#define ld long double
#define mapii map<int, int>
#define mapsi map<string, int>
#define erep(i, x, n) for (int i = x; i<=(int)(n); i++)
#define rep(i, x, n) for(int i = x; i<(int)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pie  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-9)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    int check[1] = {}, source = 0;
    vii vec(n);
    rep(i, 0, n)
    {
        cin >> vec[i].first;
        vec[i].second = i;
        if (!vec[i].first) check[0]++, source = i;
    }
    if (check[0] > 1 || !check[0])
    {
no:
        cout << -1;
    }
    else
    {
        deque< deque<int> > node (n);
        sort(all(vec));
        vii ans;
        rep(i, 0, sz(vec))
            node[vec[i].first].push_back(vec[i].second);
        bool hoba = true;
        for(int i = n-1; i>1; i--)
        {
            int up = 0, down = 0, cnt = 0;
            if (sz(node[i]))
            {
                while(up < sz(node[i-1]))
                {
                    while(cnt++ < k-1)
                    {
                        if (!node[i].empty())
                        {
                            ans.push_back({node[i-1][up], node[i][0]});
                            node[i].pop_front();
                        }
                        else break;
                    }
                    cnt = 0, up++;
                }
                if (up == sz(node[i-1]) && sz(node[i])) goto no;
            }
        }
        rep(i, 0, sz(node[1])) ans.push_back({node[0][0], node[1][i]});
        cout << sz(ans) << endl;
        rep(i, 0, sz(ans)) cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define vll vector<ll>
#define pii pair<int, int>
#define vii vector<pii>
#define vs vector<string>
#define vb vector<bool>
#define vi vector<int>
#define vd vector<double>
#define vvi vector< vector<int> >
#define vvii vector< vector< pii > >
#define ld long double
#define mapii map<int, int>
#define mapsi map<string, int>
#define erep(i, x, n) for (int i = x; i<=(int)(n); i++)
#define rep(i, x, n) for(int i = x; i<(int)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pie  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-9)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    int check[1] = {}, source = 0;
    vii vec(n);
    rep(i, 0, n)
    {
        cin >> vec[i].first;
        vec[i].second = i;
        if (!vec[i].first) check[0]++, source = i;
    }
    if (check[0] > 1 || !check[0])
    {
no:
        cout << -1;
    }
    else
    {
        deque< deque<int> > node (n);
        sort(all(vec));
        vii ans;
        rep(i, 0, sz(vec))
            node[vec[i].first].push_back(vec[i].second);
        bool hoba = true;
        for(int i = n-1; i>1; i--)
        {
            int up = 0, down = 0, cnt = 0;
            if (sz(node[i]))
            {
                while(up < sz(node[i-1]))
                {
                    while(cnt++ < k-1)
                    {
                        if (!node[i].empty())
                        {
                            ans.push_back({node[i-1][up], node[i][0]});
                            node[i].pop_front();
                        }
                        else break;
                    }
                    cnt = 0, up++;
                }
                if (up == sz(node[i-1]) && sz(node[i])) goto no;
            }
        }
        if (sz(node[1]) > k) goto no;
        rep(i, 0, sz(node[1])) ans.push_back({node[0][0], node[1][i]});
        cout << sz(ans) << endl;
        rep(i, 0, sz(ans)) cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define vll vector<ll>
#define pii pair<int, int>
#define vii vector<pii>
#define vs vector<string>
#define vb vector<bool>
#define vi vector<int>
#define vd vector<double>
#define vvi vector< vector<int> >
#define vvii vector< vector< pii > >
#define ld long double
#define mapii map<int, int>
#define mapsi map<string, int>
#define erep(i, x, n) for (int i = x; i<=(int)(n); i++)
#define rep(i, x, n) for(int i = x; i<(int)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pie  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-9)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)

// dummy solution

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    int check[1] = {}, source = 0;
    vii vec(n);
    rep(i, 0, n)
    {
        cin >> vec[i].first;
        vec[i].second = i;
        if (!vec[i].first) check[0]++, source = i;
    }
    if (check[0] > 1)
    {
no:
        cout << -1;
    }
    else
    {
        deque< deque<int> > node (n);
        sort(all(vec));
        vii ans;
        rep(i, 0, sz(vec))
            node[vec[i].first].push_back(vec[i].second);
        bool hoba = true;
        for(int i = n-1; i>1; i--)
        {
            int up = 0, down = 0, cnt = 0;
            if (sz(node[i]))
            {
                while(up < sz(node[i-1]))
                {
                    while(cnt++ < k-1)
                    {
                        if (!node[i].empty())
                        {
                            ans.push_back({node[i-1][up], node[i][0]});
                            node[i].pop_front();
                        }
                        else break;
                    }
                    cnt = 0, up++;
                }
                if (up == sz(node[i-1]) && sz(node[i])) goto no;
            }
        }
        rep(i, 0, sz(node[1])) ans.push_back({node[0][0], node[1][i]});
        cout << sz(ans) << endl;
        rep(i, 0, sz(ans)) cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define vll vector<ll>
#define pii pair<int, int>
#define vii vector<pii>
#define vs vector<string>
#define vb vector<bool>
#define vi vector<int>
#define vd vector<double>
#define vvi vector< vector<int> >
#define vvii vector< vector< pii > >
#define ld long double
#define mapii map<int, int>
#define mapsi map<string, int>
#define erep(i, x, n) for (int i = x; i<=(int)(n); i++)
#define rep(i, x, n) for(int i = x; i<(int)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pie  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-9)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    int check[1] = {}, source = 0;
    vii vec(n);
    rep(i, 0, n)
    {
        cin >> vec[i].first;
        vec[i].second = i;
        if (!vec[i].first) check[0]++, source = i;
    }
    if (check[0] > 1)
    {
no:
        cout << -1;
    }
    else
    {
        vvi node (n);
        sort(all(vec));
        vii ans;
        rep(i, 0, sz(vec))
            node[vec[i].first].push_back(vec[i].second);
        bool hoba = true;
        for(int i = n-1; i>1; i--)
        {
            int up = 0, down = 0, cnt = 0;
            if (sz(node[i]))
            {
                while(up < sz(node[i-1]))
                {
                    bool flag = true;
                    while(cnt < k && down < sz(node[i]))
                    {
                        flag = false;
                        ans.push_back({node[i-1][up], node[i][down]});
                        down++;
                    }
                    if (flag)
                        goto no;
                    cnt = 0, up++;
                }
            }
        }
        rep(i, 0, sz(node[1])) ans.push_back({node[0][0], node[1][i]});
        cout << sz(ans) << endl;
        rep(i, 0, sz(ans)) cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define vll vector<ll>
#define pii pair<int, int>
#define vii vector<pii>
#define vs vector<string>
#define vb vector<bool>
#define vi vector<int>
#define vd vector<double>
#define vvi vector< vector<int> >
#define vvii vector< vector< pii > >
#define ld long double
#define mapii map<int, int>
#define mapsi map<string, int>
#define erep(i, x, n) for (int i = x; i<=(int)(n); i++)
#define rep(i, x, n) for(int i = x; i<(int)(n); i++)
#define INF  LLONG_MAX
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define pie  acos(-1)
#define mod(n,m) ((n % m + m) % m)
#define eps (1e-9)
#define reset(n, m) memset(n, m, sizeof n)
#define endl '\n'
#define output freopen("output.txt", "w", stdout)

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    int check[1] = {}, source = 0;
    vii vec(n);
    rep(i, 0, n)
    {
        cin >> vec[i].first;
        vec[i].second = i;
        if (!vec[i].first) check[0]++, source = i;
    }
    if (check[0] > 1)
    {
no:
        cout << -1;
    }
    else
    {
        vvi node (n);
        sort(all(vec));
        vii ans;
        rep(i, 0, sz(vec))
            node[vec[i].first].push_back(vec[i].second);
        bool hoba = true;
        for(int i = n-1; i>1; i--)
        {
            int up = 0, down = 0, cnt = 0;
            if (sz(node[i]))
            {
                while(up < sz(node[i-1]))
                {
                    bool flag = true;
                    while(cnt < k && down < sz(node[i]))
                    {
                        flag = false;
                        ans.push_back({node[i-1][up], node[i][down]});
                        down++;
                    }
                    /*if (flag)
                        goto no;*/
                    cnt = 0, up++;
                }
            }
        }
        rep(i, 0, sz(node[1])) ans.push_back({node[0][0], node[1][i]});
        cout << sz(ans) << endl;
        rep(i, 0, sz(ans)) cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll d[100010];
vector <ll> g[100010];
vector <ll> v[100010];
vector <pair<ll,ll> > res;
int main() {
    ll n,k,cnt=0,s,prev=1,edges=0,val=-1;
    cin>>n>>k;
    for(ll i=0;i<n;i++)
    {
        cin>>d[i];
        //cout<<d[i]<<endl;
        val = max(val,d[i]);
        if(d[i]==0)
        {
            s=i+1;
            cnt++;
        }
        v[d[i]].push_back(i+1);
    }
    if(cnt==0||cnt>1)
    {
        //cout<<cnt<<endl;
        cout<<"-1"<<endl;
        return 0;
    }
    for(ll i=1;i<=val;i++)
    {
        // cout<<v[i].size()<<endl;
        if(v[i].size()==0)
        {
            cout<<"-1"<<endl;
            break;
        }
        if((ll)v[i].size()*prev<=k)
        {
            if(i==1)
            {
                for(ll j=0;j<v[i].size();j++)
                {
                    //cout<<"ppp"<<endl;
                    //g[s].push_back(v[i][j]);
                    //g[v[i][j]].push_back(s);
                    res.push_back(make_pair(s,v[i][j]));
                    edges+=2;
                }
            }
            else
            {
                ll l = 0;
                for(ll j=0;j<v[i-1].size();j++)
                {
                    ll temp = k;
                    while(temp--&&l<(ll)v[i].size())
                    {
                        res.push_back(make_pair(v[i][l],v[i-1][j]));
                        //g[v[i][l]].push_back(v[i-1][j]);
                        //g[v[i-1][j]].push_back(v[i][l]);
                        l++;
                        edges+=2;
                    }
                    if(l>=(ll)v[i].size())
                    break;
                }
            }
        }
        else
        {
            cout<<"-1"<<endl;
            return 0;
        }
        prev = (ll)v[i].size();
    }
    cout<<res.size()<<endl;
    for(ll i=0;i<res.size();i++)
        cout<<res[i].first<<" "<<res[i].second<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll d[100010];
vector <ll> g[100010];
vector <ll> v[100010];
vector <pair<ll,ll> > res;
int main() {
    ll n,k,cnt=0,s,prev=1,edges=0,val=-1;
    cin>>n>>k;
    for(ll i=0;i<n;i++)
    {
        cin>>d[i];
        //cout<<d[i]<<endl;
        val = max(val,d[i]);
        if(d[i]==0)
        {
            s=i+1;
            cnt++;
        }
        v[d[i]].push_back(i+1);
    }
    if(cnt==0||cnt>1)
    {
        //cout<<cnt<<endl;
        cout<<"-1"<<endl;
        return 0;
    }
    for(ll i=1;i<=val;i++)
    {
        // cout<<v[i].size()<<endl;
        if(v[i].size()==0)
        {
            cout<<"-1"<<endl;
            break;
        }
        if((ll)prev*k>=v[i].size())
        {
            if(i==1)
            {
                for(ll j=0;j<v[i].size();j++)
                {
                    //cout<<"ppp"<<endl;
                    //g[s].push_back(v[i][j]);
                    //g[v[i][j]].push_back(s);
                    res.push_back(make_pair(s,v[i][j]));
                    edges+=2;
                }
            }
            else
            {
                ll l = 0;
                for(ll j=0;j<v[i-1].size();j++)
                {
                    ll temp = k-1;
                    while(temp--&&l<(ll)v[i].size())
                    {
                        res.push_back(make_pair(v[i][l],v[i-1][j]));
                        //g[v[i][l]].push_back(v[i-1][j]);
                        //g[v[i-1][j]].push_back(v[i][l]);
                        l++;
                        edges+=2;
                    }
                    if(l>=(ll)v[i].size())
                    break;
                }
            }
        }
        else
        {
            cout<<"-1"<<endl;
            return 0;
        }
        prev = (ll)v[i].size();
    }
    cout<<res.size()<<endl;
    for(ll i=0;i<res.size();i++)
        cout<<res[i].first<<" "<<res[i].second<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll d[100010];
vector <ll> g[100010];
vector <ll> v[100010];
vector <pair<ll,ll> > res;
int main() {
    ll n,k,cnt=0,s,prev=1,edges=0,val=-1;
    cin>>n>>k;
    for(ll i=0;i<n;i++)
    {
        cin>>d[i];
        //cout<<d[i]<<endl;
        val = max(val,d[i]);
        if(d[i]==0)
        {
            s=i+1;
            cnt++;
        }
        v[d[i]].push_back(i+1);
    }
    if(cnt==0||cnt>1)
    {
        //cout<<cnt<<endl;
        cout<<"-1"<<endl;
        return 0;
    }
    for(ll i=1;i<=val;i++)
    {
        // cout<<v[i].size()<<endl;
        if(v[i].size()==0)
        {
            cout<<"-1"<<endl;
            return 0;
        }
        if((ll)prev*k>=v[i].size())
        {
            if(i==1)
            {
                for(ll j=0;j<v[i].size();j++)
                {
                    //cout<<"ppp"<<endl;
                    //g[s].push_back(v[i][j]);
                    //g[v[i][j]].push_back(s);
                    res.push_back(make_pair(s,v[i][j]));
                    edges+=2;
                }
            }
            else
            {
                ll l = 0;
                for(ll j=0;j<v[i-1].size();j++)
                {
                    ll temp = k-1;
                    while(temp--&&l<(ll)v[i].size())
                    {
                        res.push_back(make_pair(v[i][l],v[i-1][j]));
                        //g[v[i][l]].push_back(v[i-1][j]);
                        //g[v[i-1][j]].push_back(v[i][l]);
                        l++;
                        edges+=2;
                    }
                }
                if(l<=v[i].size()-1)
                {
                    cout<<"-1"<<endl;
                    return 0;
                }
            }
        }
        else
        {
            cout<<"-1"<<endl;
            return 0;
        }
        prev = (ll)v[i].size();
    }
    cout<<res.size()<<endl;
    for(ll i=0;i<res.size();i++)
        cout<<res[i].first<<" "<<res[i].second<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll d[100010];
vector <ll> g[100010];
vector <ll> v[100010];
vector <pair<ll,ll> > res;
int main() {
    ll n,k,cnt=0,s,prev=1,edges=0,val=-1;
    cin>>n>>k;
    for(ll i=0;i<n;i++)
    {
        cin>>d[i];
        //cout<<d[i]<<endl;
        val = max(val,d[i]);
        if(d[i]==0)
        {
            s=i+1;
            cnt++;
        }
        v[d[i]].push_back(i+1);
    }
    if(cnt==0||cnt>1)
    {
        //cout<<cnt<<endl;
        cout<<"-1"<<endl;
        return 0;
    }
    for(ll i=1;i<=val;i++)
    {
        // cout<<v[i].size()<<endl;
        if(v[i].size()==0)
        {
            cout<<"-1"<<endl;
            break;
        }
        if((ll)prev*k>=v[i].size())
        {
            if(i==1)
            {
                for(ll j=0;j<v[i].size();j++)
                {
                    //cout<<"ppp"<<endl;
                    //g[s].push_back(v[i][j]);
                    //g[v[i][j]].push_back(s);
                    res.push_back(make_pair(s,v[i][j]));
                    edges+=2;
                }
            }
            else
            {
                ll l = 0;
                for(ll j=0;j<v[i-1].size();j++)
                {
                    ll temp = k-1;
                    while(temp--&&l<(ll)v[i].size())
                    {
                        res.push_back(make_pair(v[i][l],v[i-1][j]));
                        //g[v[i][l]].push_back(v[i-1][j]);
                        //g[v[i-1][j]].push_back(v[i][l]);
                        l++;
                        edges+=2;
                    }
                }
                if(l<=v[i].size()-1)
                {
                    cout<<"-1"<<endl;
                    return 0;
                }
            }
        }
        else
        {
            cout<<"-1"<<endl;
            return 0;
        }
        prev = (ll)v[i].size();
    }
    cout<<res.size()<<endl;
    for(ll i=0;i<res.size();i++)
        cout<<res[i].first<<" "<<res[i].second<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll d[100010];
vector <ll> g[100010];
vector <ll> v[100010];
vector <pair<ll,ll> > res;
int main() {
    ll n,k,cnt=0,s,prev=1,edges=0,val=-1;
    cin>>n>>k;
    for(ll i=0;i<n;i++)
    {
        cin>>d[i];
        //cout<<d[i]<<endl;
        val = max(val,d[i]);
        if(d[i]==0)
        {
            s=i+1;
            cnt++;
        }
        v[d[i]].push_back(i+1);
    }
    if(cnt==0||cnt>1)
    {
        //cout<<cnt<<endl;
        cout<<"-1"<<endl;
        return 0;
    }
    for(ll i=1;i<=val;i++)
    {
        // cout<<v[i].size()<<endl;
        if(v[i].size()==0)
        {
            cout<<"-1"<<endl;
            break;
        }
        if((ll)prev*k>=v[i].size())
        {
            if(i==1)
            {
                for(ll j=0;j<v[i].size();j++)
                {
                    //cout<<"ppp"<<endl;
                    //g[s].push_back(v[i][j]);
                    //g[v[i][j]].push_back(s);
                    res.push_back(make_pair(s,v[i][j]));
                    edges+=2;
                }
            }
            else
            {
                ll l = 0;
                for(ll j=0;j<v[i-1].size();j++)
                {
                    ll temp = k-1;
                    while(temp--&&l<(ll)v[i].size())
                    {
                        res.push_back(make_pair(v[i][l],v[i-1][j]));
                        //g[v[i][l]].push_back(v[i-1][j]);
                        //g[v[i-1][j]].push_back(v[i][l]);
                        l++;
                        edges+=2;
                    }
                    if(l<=(ll)v[i].size()-1&&temp<=0)
                    {
                        cout<<"-1"<<endl;
                        return 0;
                    }
                    else if(l>=(ll)v[i].size()&&temp>0)
                    break;
                }
            }
        }
        else
        {
            cout<<"-1"<<endl;
            return 0;
        }
        prev = (ll)v[i].size();
    }
    cout<<res.size()<<endl;
    for(ll i=0;i<res.size();i++)
        cout<<res[i].first<<" "<<res[i].second<<endl;
    return 0;
}