#include <iostream>
#include <memory.h>
using namespace std;

struct node
{
	long number;
	struct node *next;
};

struct link
{
	long a, b;
};

struct node *mas[100000];
long neighbours[100000];
struct link links[1000000];

int main()
{
	long n, k;
	cin >> n >> k;
	memset(mas, 0, sizeof(struct node*));
	for (long i = 0; i < n; i++)
	{
		long t;
		cin >> t;
		struct node *p = new struct node;
		p->next = mas[t];
		p->number = i;
		mas[t] = p;
	}
	long size = 0;
	if ((mas[0] == 0) || (mas[0]->next != 0))
	{
		cout << -1;
		return 0;
	}
	memset(neighbours, 0, 100000*sizeof(long));
	for (long i = 1; i < n; i++)
	{
		struct node *p;
		p = mas[i];
		struct node *q;
		q = mas[i-1];
		while (p)
		{
			while ((q) && (neighbours[q->number] >= k))
				q = q->next;
			if (q == 0)
			{
				cout << -1;
				return 0;
			}
			neighbours[p->number]++;
			neighbours[q->number]++;
			links[size].a = p->number;
			links[size++].b = q->number;
			p = p->next;
		}
	}
	cout << size << endl;
	for (long i = 0; i < size; i++)
		cout << links[i].a+1 << ' ' << links[i].b+1 << endl;
	return 0;
}


#if 1
#include <functional>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <list>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;

const LD eps = 1e-9;
const LD pi = acos(-1.0);
const LL inf = 1e+9;

#define mp make_pair
#define pb push_back
#define X first
#define Y second

#define dbg(x) { cerr << #x << " = " << x << endl; }

// extended template
#pragma comment(linker, "/STACK:36777216")
typedef vector<int> vi;
typedef vector<vi> vvi;

#define forn(i, n) for (int i = 0; i < n; ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template<typename T> istream & operator >> (istream &, vector<T> &);
template<typename T> ostream & operator << (ostream &, const vector<T> &);

#define START clock_t _clock = clock();
#define END cerr << endl << "time: " << (clock() - _clock) / LD(CLOCKS_PER_SEC) << endl;

#define NAME "problem"

bool solve()
{
    int n, k;
    scanf("%d %d", &n, &k);
    vi d(n);
    forn(i, n)
        scanf("%d", &d[i]);

    map<int, vi> m;
    forn(i, n)
        m[d[i]].pb(i + 1);

    if (m.begin()->X)
        return false;

    vi dd; dd.reserve(m.size());

    vector<pii> res;

    vi prev;
    int t = 1;
    prev.pb(t);

    vi perm(n + 1, -1);
    perm[1] = m.begin()->Y[0];

    for (map<int, vi>::const_iterator it = ++m.begin(); it != m.end(); ++it)
    {
        dd.pb(it->X);

        const int cnt = it->Y.size();
        vi cur; cur.reserve(cnt);
        forn(i, cnt)
            cur.pb(++t);

        forn(i, cnt)
        {
            int pos = prev[0] == 1 ? i / k : (i / (k - 1));
            if (pos >= prev.size())
                return false;
            res.pb(mp(prev[pos], cur[i]));

            perm[cur[i]] = it->Y[i];
        }

        swap(prev, cur);
    }

    for (int i = 1; i < dd.size(); ++i)
        if (dd[i] - dd[i - 1] != 1)
            return false;

    if (t != n)
        return false;

    printf("%d\n", res.size());
    forn(i, res.size())
        printf("%d %d\n", perm[res[i].X], perm[res[i].Y]);

    return true;
}

int main()
{
    START
    // freopen(NAME ".in", "r", stdin); freopen(NAME ".out", "w", stdout);
    //freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);

    if (!solve())
        cout << -1 << endl;

    END
    return 0;
}
/*******************************************
*******************************************/

template<typename T> istream & operator >> (istream &is, vector<T> &v)
{
    forn(i, v.size())
        is >> v[i];
    return is;
}
template<typename T> ostream & operator << (ostream &os, const vector<T> &v)
{
    forn(i, v.size())
        os << v[i] << " ";
    return os;
}
#endif


#if 1
#include <functional>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <list>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;

const LD eps = 1e-9;
const LD pi = acos(-1.0);
const LL inf = 1e+9;

#define mp make_pair
#define pb push_back
#define X first
#define Y second

#define dbg(x) { cerr << #x << " = " << x << endl; }

// extended template
#pragma comment(linker, "/STACK:36777216")
typedef vector<int> vi;
typedef vector<vi> vvi;

#define forn(i, n) for (int i = 0; i < n; ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template<typename T> istream & operator >> (istream &, vector<T> &);
template<typename T> ostream & operator << (ostream &, const vector<T> &);

#define START clock_t _clock = clock();
#define END cerr << endl << "time: " << (clock() - _clock) / LD(CLOCKS_PER_SEC) << endl;

#define NAME "problem"

bool solve()
{
    int n, k;
    scanf("%d %d", &n, &k);
    vi d(n);
    forn(i, n)
        scanf("%d", &d[i]);

    map<int, vi> m;
    forn(i, n)
        m[d[i]].pb(i + 1);

    if (m.begin()->X)
        return false;

    vi dd; dd.reserve(m.size());

    vector<pii> res;

    vi prev;
    int t = 1;
    prev.pb(t);
    vi cur;

    vi perm(n + 1, -1);
    perm[1] = m.begin()->Y[0];

    for (map<int, vi>::const_iterator it = ++m.begin(); it != m.end(); ++it)
    {
        dd.pb(it->X);

        const int cnt = it->Y.size();
        cur.clear();
        forn(i, cnt)
            cur.pb(++t);

        forn(i, cnt)
        {
            int pos;
            if (prev[0] == 1)
                pos = i / k;
            else
            {
                if (k == 1)
                    return false;
                pos = i / (k - 1);
            }

            if (pos >= prev.size())
                return false;
            res.pb(mp(prev[pos], cur[i]));

            perm[cur[i]] = it->Y[i];
        }

        swap(prev, cur);
    }

    for (int i = 1; i < dd.size(); ++i)
        if (dd[i] - dd[i - 1] != 1)
            return false;

    if (t != n)
        return false;

    printf("%d\n", res.size());
    forn(i, res.size())
        printf("%d %d\n", perm[res[i].X], perm[res[i].Y]);

    return true;
}

int main()
{
    START
    // freopen(NAME ".in", "r", stdin); freopen(NAME ".out", "w", stdout);
    //freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);

    if (!solve())
        cout << -1 << endl;

    END
    return 0;
}
/*******************************************
*******************************************/

template<typename T> istream & operator >> (istream &is, vector<T> &v)
{
    forn(i, v.size())
        is >> v[i];
    return is;
}
template<typename T> ostream & operator << (ostream &os, const vector<T> &v)
{
    forn(i, v.size())
        os << v[i] << " ";
    return os;
}
#endif


#if 1
#include <functional>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <list>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> pii;

const LD eps = 1e-9;
const LD pi = acos(-1.0);
const LL inf = 1e+9;

#define mp make_pair
#define pb push_back
#define X first
#define Y second

#define dbg(x) { cerr << #x << " = " << x << endl; }

// extended template
#pragma comment(linker, "/STACK:36777216")
typedef vector<int> vi;
typedef vector<vi> vvi;

#define forn(i, n) for (int i = 0; i < n; ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template<typename T> istream & operator >> (istream &, vector<T> &);
template<typename T> ostream & operator << (ostream &, const vector<T> &);

#define START clock_t _clock = clock();
#define END cerr << endl << "time: " << (clock() - _clock) / LD(CLOCKS_PER_SEC) << endl;

#define NAME "problem"

bool solve()
{
    int n, k;
    scanf("%d %d", &n, &k);
    vi d(n);
    forn(i, n)
        scanf("%d", &d[i]);

    map<int, vi> m;
    forn(i, n)
        m[d[i]].pb(i + 1);

    if (m.begin()->X)
        return false;

    vi dd; dd.reserve(m.size());

    vector<pii> res;

    vi prev;
    int t = 1;
    prev.pb(t);

    vi perm(n + 1, -1);
    perm[1] = m.begin()->Y[0];

    for (map<int, vi>::const_iterator it = ++m.begin(); it != m.end(); ++it)
    {
        dd.pb(it->X);

        const int cnt = it->Y.size();
        vi cur; cur.reserve(cnt);
        forn(i, cnt)
            cur.pb(++t);

        forn(i, cnt)
        {
            int pos = i / k;
            if (pos >= prev.size())
                return false;
            res.pb(mp(prev[pos], cur[i]));

            perm[cur[i]] = it->Y[i];
        }

        swap(prev, cur);
    }

    for (int i = 1; i < dd.size(); ++i)
        if (dd[i] - dd[i - 1] != 1)
            return false;

    if (t != n)
        return false;

    printf("%d\n", res.size());
    forn(i, res.size())
        printf("%d %d\n", perm[res[i].X], perm[res[i].Y]);

    return true;
}

int main()
{
    START
    // freopen(NAME ".in", "r", stdin); freopen(NAME ".out", "w", stdout);
    //freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);

    if (!solve())
        cout << -1 << endl;

    END
    return 0;
}
/*******************************************
*******************************************/

template<typename T> istream & operator >> (istream &is, vector<T> &v)
{
    forn(i, v.size())
        is >> v[i];
    return is;
}
template<typename T> ostream & operator << (ostream &os, const vector<T> &v)
{
    forn(i, v.size())
        os << v[i] << " ";
    return os;
}
#endif


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define max 1000010
using namespace std;
struct k{
    int z[2];
};
struct k distan[max], map[max];

int a, mi, process;
int tree(int first, int last, int change)
{
    int i, j, sign;
    if (distan[last].z[0] != change || distan[first].z[0] != change - 1) return -1;
    for (i = first, j = last; i < last; i++)
    {
        for (sign = 0; distan[j].z[0] == change && sign < mi; j++) map[process].z[0] = distan[i].z[1], map[process++].z[1] = distan[j].z[1], sign++;
    }
    if (j == a + 1) return process - 1;
    else if (distan[j].z[0] != change + 1) return -1;
    else
    {
        if (first == 1) mi -= 1;
        return tree(i, j, change + 1);
    }
}

bool cmp(struct k &a, struct k &b)
{
    if (a.z[0] > b.z[0]) return false;
    else if (a.z[0] < b.z[0]) return true;
    else
    {
        if (a.z[1] > b.z[1]) return false;
        else return true;
    }
}

int main()
{
    int b, i, sign = 0;
    scanf("%d%d", &a, &mi);
    memset(map, -1, sizeof(map));
    for (i = 1; i <= a; i++)
    {
        distan[i].z[1] = i, distan[i + 1].z[0] = -1;
        scanf("%d", &distan[i].z[0]);
    }
    process = 0;
    sort(distan + 1, distan + a + 1, cmp);
    b = tree(1, 2, 1);
    if (b == -1) printf("-1\n");
    else
    {
        printf("%d\n", b + 1);
        for (i = 0; map[i].z[0] != -1; i++)
        {
            if (map[i].z[0] > map[i].z[1])
            {
                b = map[i].z[0];
                map[i].z[0] = map[i].z[1];
                map[i].z[1] = b;
            }
            sign++;
        }
        sort(map, map + sign, cmp);
        for (i = 0; i < sign; i++) printf("%d %d\n", map[i].z[0], map[i].z[1]);
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define max 1000010
using namespace std;
struct k{
	int z[2];
};
struct k distan[max], map[max];

int a, mi, process;
int tree(int first, int last, int change)
{
	int i, j, sign;
	if (distan[last].z[0] != change || distan[first].z[0] != change - 1) return 0;
	for (i = first, j = last; i < last; i++)
	{
		for (sign = 0; distan[j].z[0] == change && sign <= mi; j++) map[process].z[0] = distan[i].z[1], map[process++].z[1] = distan[j].z[1], sign++;
	}
	if (j == a + 1) return process - 1;
	else if (distan[j].z[0] != change + 1) return 0;
	else return tree(i, j, change + 1);
}

bool cmp(struct k &a, struct k &b)
{
	if (a.z[0] > b.z[0]) return false;
	else if (a.z[0] < b.z[0]) return true;
	else
	{
		if (a.z[1] > b.z[1]) return false;
		else return true;
	}
}
bool comp(int * a, int * b)
{
	if (*a > *b) return false;
	else return true;
}

int main()
{
	int b, i, sign = 0;
	scanf("%d%d", &a, &mi);
	memset(map, -1, sizeof(map));
	for (i = 1; i <= a; i++)
	{
		distan[i].z[1] = i, distan[i + 1].z[0] = -1;
		scanf("%d", &distan[i].z[0]);
	}
	process = 0;
	sort(distan + 1, distan + a + 1, cmp);
	b = tree(1, 2, 1);
	if (b == 0) printf("-1\n");
	else
	{
		printf("%d\n", b + 1);
		for (i = 0; map[i].z[0] != -1; i++)
		{
			if (map[i].z[0] > map[i].z[1])
			{
				b = map[i].z[0];
				map[i].z[0] = map[i].z[1];
				map[i].z[1] = b;
			}
			sign++;
		}
		sort(map, map + sign, cmp);
		for (i = 0; i < sign; i++) printf("%d %d\n", map[i].z[0], map[i].z[1]);
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define max 1000010
using namespace std;
struct k{
	int z[2];
};
struct k distan[max], map[max];

int a, mi, process;
int tree(int first, int last, int change)
{
	int i, j, sign;
	if (distan[last].z[0] != change || distan[first].z[0] != change - 1) return 0;
	for (i = first, j = last; i < last; i++)
	{
		for (sign = 0; distan[j].z[0] == change && sign <= mi; j++) map[process].z[0] = distan[i].z[1], map[process++].z[1] = distan[j].z[1], sign++;
	}
	if (j == a + 1) return process - 1;
	else if (distan[j].z[0] != change + 1) return 0;
	else return tree(i, j, change + 1);
}

bool cmp(struct k &a, struct k &b)
{
	if (a.z[0] > b.z[0]) return false;
	else if (a.z[0] < b.z[0]) return true;
	else
	{
		if (a.z[1] > b.z[1]) return false;
		else return true;
	}
}
bool comp(struct k &a, struct k &b)
{
	if (a.z[0] > b.z[0]) return false;
	else if (a.z[0] < b.z[0]) return true;
	else
	{
		if (a.z[1] < b.z[1]) return false;
		else return true;
	}
}
int main()
{
	int b, i, sign = 0;
	scanf("%d%d", &a, &mi);
	memset(map, -1, sizeof(map));
	for (i = 1; i <= a; i++)
	{
		distan[i].z[1] = i, distan[i + 1].z[0] = -1;
		scanf("%d", &distan[i].z[0]);
	}
	process = 0;
	sort(distan + 1, distan + a + 1, comp);
	b = tree(1, 2, 1);
	if (b == 0) printf("-1\n");
	else
	{
		printf("%d\n", b + 1);
		for (i = 0; map[i].z[0] != -1; i++)
		{
			if (map[i].z[0] > map[i].z[1])
			{
				b = map[i].z[0];
				map[i].z[0] = map[i].z[1];
				map[i].z[1] = b;
			}
			sign++;
		}
		sort(map, map + sign, cmp);
		for (i = 0; i < sign; i++) printf("%d %d\n", map[i].z[0], map[i].z[1]);
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define max 1000010
using namespace std;
struct k{
    int z[2];
};
struct k distan[max], map[max];

int a, mi, process;
int tree(int first, int last, int change)
{
    int i, j, sign;
    if (distan[last].z[0] != change || distan[first].z[0] != change - 1) return 0;
    for (i = first, j = last; i < last; i++)
    {
        for (sign = 0; distan[j].z[0] == change && sign < mi; j++) map[process].z[0] = distan[i].z[1], map[process++].z[1] = distan[j].z[1], sign++;
    }
    if (j == a + 1) return process - 1;
    else if (distan[j].z[0] != change + 1) return 0;
    else
    {
        if (first == 1) mi -= 1;
        return tree(i, j, change + 1);
    }
}

bool cmp(struct k &a, struct k &b)
{
    if (a.z[0] > b.z[0]) return false;
    else if (a.z[0] < b.z[0]) return true;
    else
    {
        if (a.z[1] > b.z[1]) return false;
        else return true;
    }
}

int main()
{
    int b, i, sign = 0;
    scanf("%d%d", &a, &mi);
    memset(map, -1, sizeof(map));
    for (i = 1; i <= a; i++)
    {
        distan[i].z[1] = i, distan[i + 1].z[0] = -1;
        scanf("%d", &distan[i].z[0]);
    }
    process = 0;
    sort(distan + 1, distan + a + 1, cmp);
    b = tree(1, 2, 1);
    if (b == 0) printf("-1\n");
    else
    {
        printf("%d\n", b + 1);
        for (i = 0; map[i].z[0] != -1; i++)
        {
            if (map[i].z[0] > map[i].z[1])
            {
                b = map[i].z[0];
                map[i].z[0] = map[i].z[1];
                map[i].z[1] = b;
            }
            sign++;
        }
        sort(map, map + sign, cmp);
        for (i = 0; i < sign; i++) printf("%d %d\n", map[i].z[0], map[i].z[1]);
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
class dato{
public:
    long d,g,i;
   };
bool operator <(dato a, dato b){
    return(a.d<b.d || (a.d==b.d && (a.i<b.i)));
};
dato  arr[100010];
long a[1000010][2], i,k,n,q,izq,der;
bool sepuede=true;
int main(){
   scanf("%ld%ld",&n,&k);
   for(i=0;i<n;i++){
      scanf("%ld",&arr[i].d);
      arr[i].g=k;
      arr[i].i=i;
   }
   sort(arr,arr+n);
   if(arr[0].d!=0){
          sepuede=false;
      }
   k=0;
   if(sepuede)
   for (i=1;i<n;i++){
       //if(arr[i].g>0
      while(arr[i].d!=arr[k].d+1 && k<i)
          k++;
      if(k>=i){
          sepuede=false;
          break;
      }
      if(arr[i].g>0 && arr[i].d==arr[k].d+1 && arr[k].g>0){
        a[q][0]=arr[k].i+1;
        a[q][1]=arr[i].i+1;
        arr[i].g--;
        arr[k].g--;
        q++;
        if(arr[k].g==0)
            k++;
      }
      else{
          sepuede=false;
          break;
      }
   }
   if(sepuede){
       printf("%ld\n",q);
      for(i=0;i<q;i++)
         printf("%ld %ld\n",a[i][0],a[i][1]);

   }
   else
      printf("-1\n");
}


#include<stdio.h>
#include<algorithm>
using namespace std;
class dato{
public:
    long d,g,i;
   };
bool operator <(dato a, dato b){
    return(a.d<b.d || (a.d==b.d && (a.i<b.i)));
};
dato  arr[100010];
long a[1000010][2], i,k,n,q,izq,der;
bool sepuede=true;
int main(){
   scanf("%ld%ld",&n,&k);
   for(i=0;i<n;i++){
      scanf("%ld",&arr[i].d);
      arr[i].g=k;
      arr[i].i=i;
   }
   sort(arr,arr+n);
   if(arr[0].g!=0){
          sepuede=false;
      }
   k=0;
   if(sepuede)
   for (i=1;i<n;i++){
       //if(arr[i].g>0
      while(arr[i].d!=arr[k].d+1 && k<i)
          k++;
      if(k>=i){
          sepuede=false;
          break;
      }
      if(arr[i].g>0 && arr[i].d==arr[k].d+1 && arr[k].g>0){
        a[q][0]=arr[k].i+1;
        a[q][1]=arr[i].i+1;
        arr[i].g--;
        arr[k].g--;
        q++;
        if(arr[k].g==0)
            k++;
      }
      else{
          sepuede=false;
          break;
      }
   }
   if(sepuede){
       printf("%ld\n",q);
      for(i=0;i<q;i++)
         printf("%ld %ld\n",a[i][0],a[i][1]);

   }
   else
      printf("-1\n");
}


#include<stdio.h>
#include<algorithm>
using namespace std;
class dato{
public:
    long d,g,i;
   };
bool operator <(dato a, dato b){
    return(a.d<b.d || (a.d==b.d && (a.i<b.i)));
};
dato  arr[100010];
long a[1000010][2], i,k,n,q,izq,der;
bool sepuede=true;
int main(){
   scanf("%ld%ld",&n,&k);
   for(i=0;i<n;i++){
      scanf("%ld",&arr[i].d);
      arr[i].g=k;
      arr[i].i=i;
   }
   sort(arr,arr+n);
   k=0;
   for (i=1;i<n;i++){
       //if(arr[i].g>0
      while(arr[i].d!=arr[k].d+1 && k<i)
          k++;
      if(k>=i){
          sepuede=false;
          break;
      }
      if(arr[i].g>0 && arr[i].d==arr[k].d+1 && arr[k].g>0){
        a[q][0]=arr[k].i+1;
        a[q][1]=arr[i].i+1;
        arr[i].g--;
        arr[k].g--;
        q++;
        if(arr[k].g==0)
            k++;
      }
      else{
          sepuede=false;
          break;
      }
   }
   if(sepuede){
       printf("%ld\n",q);
      for(i=0;i<q;i++)
         printf("%ld %ld\n",a[i][0],a[i][1]);

   }
   else
      printf("-1\n");
}


#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
typedef long long ll;
#define mset(a, val) memset(a, val, sizeof(a))
#define up(i, s, t) for (ll i = (s); i < (t); i += 1)
#define down(i, s, t) for (ll i = (s); i > (t); i -= 1)
#define rd1(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d %d", &a, &b)
#define rd3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define rd4(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pii pair<int, int>

using namespace std;
const int MAXINT = 0x6fffffff;
const ll MAXLONG = (ll) 1 << 63 - 1;
const int MAXM = 1e5 + 5;

vector<int> dd[MAXM];
ll n, k;

int main () {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

	while (cin >> n >> k) {

		up(i, 0, n + 1) {
			dd[i].clear();
		}

		up(i, 1, n + 1) {
			int d;

			rd1(d);
			dd[d].push_back(i);
		}

		bool ok = true;
		if (dd[0].size() != 1) {
			puts("-1");
			continue;
		}

		up(i, 1, n + 1) {
			ll can = dd[i - 1].size() * k;
			if (can < dd[i].size()) {
				ok = false;
				break;
			}

		}

		if (!ok) {
			puts("-1");
			continue;
		}

		printf("%d\n", n - 1);
		up(i, 1, n + 1) {
			int pre_sz = dd[i - 1].size();
			int idx = 0;
			up(j, 0, pre_sz) {
				up(h, 0, k) {
					if (idx >= dd[i].size()) {
						break;
					}
					printf("%d %d\n", dd[i - 1][j], dd[i][idx]);
					idx ++;
				}
			}
		}
	}
    return 0;
}


#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
typedef long long ll;
#define mset(a, val) memset(a, val, sizeof(a))
#define up(i, s, t) for (ll i = (s); i < (t); i += 1)
#define down(i, s, t) for (ll i = (s); i > (t); i -= 1)
#define rd1(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d %d", &a, &b)
#define rd3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define rd4(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pii pair<int, int>

using namespace std;
const int MAXINT = 0x6fffffff;
const ll MAXLONG = (ll) 1 << 63 - 1;
const int MAXM = 1e5 + 5;

vector<int> dd[MAXM];
ll n, k;

int main () {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

	while (cin >> n >> k) {

		up(i, 0, n + 1) {
			dd[i].clear();
		}

		up(i, 1, n + 1) {
			int d;

			rd1(d);
			dd[d].push_back(i);
		}

		bool ok = true;
		if (dd[0].size() != 1) {
			puts("-1");
			continue;
		}

		up(i, 1, n + 1) {
			ll can = dd[i - 1].size() * (k - 1);
			if (can < dd[i].size()) {
				ok = false;
				break;
			}

		}

		if (!ok) {
			puts("-1");
			continue;
		}

		printf("%d\n", n - 1);
		up(i, 1, n + 1) {
			int pre_sz = dd[i - 1].size();
			int idx = 0;
			up(j, 0, pre_sz) {
				up(h, 0, k) {
					if (idx >= dd[i].size()) {
						break;
					}
					printf("%d %d\n", dd[i - 1][j], dd[i][idx]);
					idx ++;
				}
			}
		}
	}
    return 0;
}


#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
typedef long long ll;
#define mset(a, val) memset(a, val, sizeof(a))
#define up(i, s, t) for (ll i = (s); i < (t); i += 1)
#define down(i, s, t) for (ll i = (s); i > (t); i -= 1)
#define rd1(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d %d", &a, &b)
#define rd3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define rd4(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pii pair<int, int>

using namespace std;
const int MAXINT = 0x6fffffff;
const ll MAXLONG = (ll) 1 << 63 - 1;
const int MAXM = 1e5 + 5;

vector<int> dd[MAXM];
ll n, k;

int main () {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

	while (cin >> n >> k) {

		up(i, 0, n + 1) {
			dd[i].clear();
		}

		up(i, 1, n + 1) {
			int d;

			rd1(d);
			dd[d].push_back(i);
		}

		bool ok = true;
		if (dd[0].size() != 1) {
			puts("-1");
			continue;
		}

		up(i, 1, n + 1) {

			ll can = dd[i - 1].size() * k;
			if (i > 1) {
				can -= dd[i - 1].size();
			}
			if (can < dd[i].size()) {
				ok = false;
				break;
			}

		}

		if (!ok) {
			puts("-1");
			continue;
		}

		printf("%d\n", n - 1);
		up(i, 1, n + 1) {
			int pre_sz = dd[i - 1].size();
			int idx = 0;
			up(j, 0, pre_sz) {
				up(h, 0, k) {
					if (idx >= dd[i].size()) {
						break;
					}
					printf("%d %d\n", dd[i - 1][j], dd[i][idx]);
					idx ++;
				}
			}
		}
	}
    return 0;
}


#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
typedef long long ll;
#define mset(a, val) memset(a, val, sizeof(a))
#define up(i, s, t) for (ll i = (s); i < (t); i += 1)
#define down(i, s, t) for (ll i = (s); i > (t); i -= 1)
#define rd1(a) scanf("%d", &a)
#define rd2(a, b) scanf("%d %d", &a, &b)
#define rd3(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define rd4(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pii pair<int, int>

using namespace std;
const int MAXINT = 0x6fffffff;
const ll MAXLONG = (ll) 1 << 63 - 1;
const int MAXM = 1e5 + 5;

vector<int> dd[MAXM];
ll n, k;

int main () {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

	while (cin >> n >> k) {

		up(i, 0, n + 1) {
			dd[i].clear();
		}

		up(i, 1, n + 1) {
			int d;

			rd1(d);
			dd[d].push_back(i);
		}

		bool ok = true;
		if (dd[0].size() != 1) {
			puts("-1");
			continue;
		}

		up(i, 1, n + 1) {

			ll can = dd[i - 1].size() * k;
			if (i > 1) {
				can -= dd[i - 1].size();
			}
			if (can < dd[i].size()) {
				ok = false;
				break;
			}

		}

		if (!ok) {
			puts("-1");
			continue;
		}

		printf("%d\n", n - 1);
		up(i, 1, n + 1) {
			int pre_sz = dd[i - 1].size();
			int idx = 0;
			up(j, 0, pre_sz) {
				int most = i == 1 ? k : k - 1;
				up(h, 0, most) { // at most k edges
					if (idx >= dd[i].size()) {
						break;
					}
					printf("%d %d\n", dd[i - 1][j], dd[i][idx]);
					idx ++;
				}
			}
		}
	}
    return 0;
}


#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;

int n, k, cnt[1000000];

pair<int, int> d[1000001];
stack < pair<int , int> > ans;

bool comp(pair<int, int> i, pair<int, int> j)
{
	return i.first < j.first;
}

int main()
{
	scanf("%d %d",&n,&k);
	for(int i=1; i<=n; i++)
	{
		scanf("%d",&d[i].first);
		d[i].second = i;
	}

	sort(d+1, d+1+n, comp);

	int l = 1;
	int r = 2;
	int tup = 0;
	for(int i=1; i<=n; i++)
	{
		if(d[i].first == 0)
			tup++;
	}

	if(tup != 1)
	{
		printf("-1");
		exit(0);
	}

	while(r <= n)
	{
		while(l < r && d[l].first + 1 < d[r].first || cnt[d[l].second] == k)
			l++;
		if(l == r || d[l].first + 1 != d[r].first)
		{
			printf("-1");
			exit(0);
		}
		ans.push(make_pair(d[l].second, d[r].second));
		cnt[d[l].second]++;
		r++;
	}

	printf("%d\n",ans.size());
	while(!ans.empty())
	{
		pair<int, int> p = ans.top();
		ans.pop();
		printf("%d %d\n",p.first, p.second);
	}

return 0;
}


#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;

int n, k, cnt[1000000];

pair<int, int> d[100001];
stack < pair<int , int> > ans;

bool comp(pair<int, int> i, pair<int, int> j)
{
	return i.first < j.first;
}

int main()
{
	scanf("%d %d",&n,&k);
	for(int i=1; i<=n; i++)
	{
		scanf("%d",&d[i].first);
		d[i].second = i;
	}

	sort(d+1, d+1+n, comp);

	int l = 1;
	int r = 2;
	int tup = 0;
	for(int i=1; i<=n; i++)
	{
		if(d[i].first == 0)
			tup++;
	}

	if(tup != 1)
	{
		printf("-1");
		exit(0);
	}
	while(r <= n)
	{
		while(l < r && d[l].first + 1 < d[r].first || cnt[d[l].second] == k)
			l++;
		if(l == r)
		{
			printf("-1");
			exit(0);
		}
		ans.push(make_pair(d[l].second, d[r].second));
		cnt[d[l].second]++;
		r++;
	}

	printf("%d\n",ans.size());
	while(!ans.empty())
	{
		pair<int, int> p = ans.top();
		ans.pop();
		printf("%d %d\n",p.first, p.second);
	}

return 0;
}


#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;

int n, k, cnt[1000000];

pair<int, int> d[100001];
stack < pair<int , int> > ans;

bool comp(pair<int, int> i, pair<int, int> j)
{
	return i.first < j.first;
}

int main()
{
	scanf("%d %d",&n,&k);
	for(int i=1; i<=n; i++)
	{
		scanf("%d",&d[i].first);
		d[i].second = i;
	}

	sort(d+1, d+1+n, comp);

	int l = 1;
	int r = 2;
	int tup = 0;
	for(int i=1; i<=n; i++)
	{
		if(d[i].first == 0)
			tup++;
	}

	if(tup != 1)
	{
		printf("-1");
		exit(0);
	}
	while(r <= n)
	{
		while(l < r && d[l].first + 1 < d[r].first || cnt[d[l].second] == k)
			l++;
		if(l == r)
		{
			printf("-1");
			exit(0);
		}
		ans.push(make_pair(d[l].second, d[r].second));
		r++;
	}

	printf("%d\n",ans.size());
	while(!ans.empty())
	{
		pair<int, int> p = ans.top();
		ans.pop();
		printf("%d %d\n",p.first, p.second);
	}

return 0;
}


#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;

int n, k, cnt[1000000];

pair<int, int> d[1000001];
stack < pair<int , int> > ans;

bool comp(pair<int, int> i, pair<int, int> j)
{
	return i.first < j.first;
}

int main()
{
	scanf("%d %d",&n,&k);
	for(int i=1; i<=n; i++)
	{
		scanf("%d",&d[i].first);
		d[i].second = i;
	}

	sort(d+1, d+1+n, comp);

	int l = 1;
	int r = 2;
	int tup = 0;
	for(int i=1; i<=n; i++)
	{
		if(d[i].first == 0)
			tup++;
	}

	if(tup != 1)
	{
		printf("-1");
		exit(0);
	}

	while(r <= n)
	{
		while(l < r && (d[l].first + 1 < d[r].first || cnt[d[l].second] == k))
			l++;
		if(l == r || d[l].first + 1 != d[r].first)
		{
			printf("-1");
			exit(0);
		}
		ans.push(make_pair(d[l].second, d[r].second));
		cnt[d[l].second]++;
		r++;
	}

	printf("%d\n",ans.size());
	while(!ans.empty())
	{
		pair<int, int> p = ans.top();
		ans.pop();
		printf("%d %d\n",p.first, p.second);
	}

return 0;
}


#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;

int n, k, cnt[1000000];

pair<int, int> d[1000001];
stack < pair<int , int> > ans;

bool comp(pair<int, int> i, pair<int, int> j)
{
	return i.first < j.first;
}

int main()
{
	scanf("%d %d",&n,&k);
	for(int i=1; i<=n; i++)
	{
		scanf("%d",&d[i].first);
		d[i].second = i;
	}

	sort(d+1, d+1+n, comp);

	int l = 1;
	int r = 2;
	int tup = 0;
	for(int i=1; i<=n; i++)
	{
		if(d[i].first == 0)
			tup++;
	}

	if(tup != 1)
	{
		printf("-1");
		exit(0);
	}

	while(r <= n)
	{
		while(l < r && (d[l].first + 1 < d[r].first || cnt[d[l].second] == k))
			l++;
		if(l == r || d[l].first + 1 != d[r].first)
		{
			printf("-1");
			exit(0);
		}
		ans.push(make_pair(d[l].second, d[r].second));
		cnt[d[l].second]++;
		cnt[d[r].second]++;
		r++;
	}

	printf("%d\n",ans.size());
	while(!ans.empty())
	{
		pair<int, int> p = ans.top();
		ans.pop();
		printf("%d %d\n",p.first, p.second);
	}

return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    pair<int,int>* d = new pair<int,int>[n];
    int* N = new int[n];
    int max = 0;
    int zero = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> d[i].first;
        d[i].second = i;
        N[i] = 0;
        if (d[i].first == 0)
            zero++;
        if (d[i].first>max)
            max = d[i].first;
    }

    sort(d, d + n);

    int cur = d[0].first;
    for (int i = 1; i < n; i++)
    {
        if (d[i].first!=cur)
        if (d[i].first - 1 == cur)
            cur = d[i].first;
        else
        {
            cout << -1;
            return 0;
        }
    }

    vector<pair<int, int>> ed;

    int I = 0;
    int i, j, l;

    int F, S, T;
    F = 0;
    S = 0;
    T = 0;

    while ((d[S].first == d[F].first) && (S<n))
    {
        ++S;
    }
    T = S;
    while ((d[T].first == d[S].first) && (T<n))
    {
        ++T;
    }

    for (i = 1; i <= max; ++i)
    {
        if (d[S].first != i)
            continue;
        for (j = S; j < T; ++j)
        {
/*          if (d[j] == i)
            {
                I = -1;
                for (l = 0; l < n; l++)
                {
                    if ((d[l] == i - 1) && (N[l] < k))
                    {
                        I = l;
                    }
                }
                if (I != -1)
                {
                    //                  cout << I+1 << ' ' << j+1 << endl;
                    ed.push_back(make_pair(I + 1, j + 1));
                    ++N[I];
                    ++N[j];
                }
                else
                {
                    cout << -1;
                    return 0;
                }
            }
*/
            if (d[j].first != i)
                cout << "SHIT" << endl;

            I = -1;
            for (l = F; l < S; l++)
            {
                if ((d[l].first == i - 1) && (N[d[l].second] < k))
                {
                    I = l;
                }
            }
            if (I != -1)
            {
                //                  cout << I+1 << ' ' << j+1 << endl;
                ed.push_back(make_pair(d[I].second + 1, d[j].second + 1));
                ++N[d[I].second];
                ++N[d[j].second];
            }
            else
            {
                cout << -1;
                return 0;
            }
        }

        F = S; S = T;
//      while ((d[S].first == d[F].first) && (S<n))
//      {
//          ++S;
//      }
        while ((d[T].first == d[S].first) && (T<n))
        {
            ++T;
        }

    }

    if (zero > 1)
    {
        cout << -1;
        return 0;
    }

    if ((n == 1) && (d[0].first != 0))
    {
        cout << -1;
        return 0;
    }

    j = ed.size();
    cout << j << endl;
    for (i = 0; i < j; i++)
        cout << ed[i].first << ' ' << ed[i].second << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <vector>
using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    int* d = new int[n];
    int* N = new int[n];
    int max = 0;
    int zero = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> d[i];
        N[i] = 0;
        if (d[i] == 0)
            zero++;
        if (d[i]>max)
            max = d[i];
    }

    vector<pair<int, int>> ed;

    int I = 0;
    int i, j, l;
    for (i = 1; i <= max; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            if (d[j] == i)
            {
                I = -1;
                for (l = 0; l < n; l++)
                {
                    if ((d[l] == i - 1) && (N[l] < k))
                    {
                        I = l;
                    }
                }
                if (I != -1)
                {
                    //                  cout << I+1 << ' ' << j+1 << endl;
                    ed.push_back(make_pair(I + 1, j + 1));
                    ++N[I];
                    ++N[j];
                }
                else
                {
                    cout << -1;
                    return 0;
                }
            }

        }
    }

    if (zero > 1)
    {
        cout << -1;
        return 0;
    }

    j = ed.size();
    cout << j << endl;
    for (i = 0; i < j; i++)
        cout << ed[i].first << ' ' << ed[i].second << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    pair<int,int>* d = new pair<int,int>[n];
    int* N = new int[n];
    int max = 0;
    int zero = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> d[i].first;
        d[i].second = i;
        N[i] = 0;
        if (d[i].first == 0)
            zero++;
        if (d[i].first>max)
            max = d[i].first;
    }

    sort(d, d + n);

    int cur = d[0].first;
    for (int i = 1; i < n; i++)
    {
        if (d[i].first!=cur)
        if (d[i].first - 1 == cur)
            cur = d[i].first;
        else
        {
            cout << -1;
            return 0;
        }
    }

    vector<pair<int, int>> ed;

    int I = 0;
    int i, j, l;

    int F, S, T;
    F = 0;
    S = 0;
    T = 0;

    while ((d[S].first == d[F].first) && (S<n))
    {
        ++S;
    }
    T = S;
    while ((d[T].first == d[S].first) && (T<n))
    {
        ++T;
    }

    for (i = 1; i <= max; ++i)
    {
        if (d[S].first != i)
            continue;
        for (j = S; j < T; ++j)
        {
/*          if (d[j] == i)
            {
                I = -1;
                for (l = 0; l < n; l++)
                {
                    if ((d[l] == i - 1) && (N[l] < k))
                    {
                        I = l;
                    }
                }
                if (I != -1)
                {
                    //                  cout << I+1 << ' ' << j+1 << endl;
                    ed.push_back(make_pair(I + 1, j + 1));
                    ++N[I];
                    ++N[j];
                }
                else
                {
                    cout << -1;
                    return 0;
                }
            }
*/
//          if (d[j].first != i)
//              cout << "SHIT" << endl;

            I = -1;
            for (l = F; l < S; l++)
            {
                if ((d[l].first == i - 1) && (N[d[l].second] < k))
                {
                    I = l;
                    break;
                }
            }
            if (I != -1)
            {
                //                  cout << I+1 << ' ' << j+1 << endl;
                ed.push_back(make_pair(d[I].second + 1, d[j].second + 1));
                ++N[d[I].second];
                ++N[d[j].second];
            }
            else
            {
                cout << -1;
                return 0;
            }
        }

        F = S; S = T;
//      while ((d[S].first == d[F].first) && (S<n))
//      {
//          ++S;
//      }
        while ((d[T].first == d[S].first) && (T<n))
        {
            ++T;
        }

    }

    if (zero > 1)
    {
        cout << -1;
        return 0;
    }

    if ((n == 1) && (d[0].first != 0))
    {
        cout << -1;
        return 0;
    }

    j = ed.size();
    cout << j << endl;
    for (i = 0; i < j; i++)
        cout << ed[i].first << ' ' << ed[i].second << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    pair<int,int>* d = new pair<int,int>[n];
    int* N = new int[n];
    int max = 0;
    int zero = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> d[i].first;
        d[i].second = i;
        N[i] = 0;
        if (d[i].first == 0)
            zero++;
        if (d[i].first>max)
            max = d[i].first;
    }

    sort(d, d + n);

    vector<pair<int, int>> ed;

    int I = 0;
    int i, j, l;

    int F, S, T;
    F = 0;
    S = 0;
    T = 0;

    while ((d[S].first == d[F].first) && (S<n))
    {
        ++S;
    }
    T = S;
    while ((d[T].first == d[S].first) && (T<n))
    {
        ++T;
    }

    for (i = 1; i <= max; ++i)
    {
        for (j = S; j < T; ++j)
        {
/*          if (d[j] == i)
            {
                I = -1;
                for (l = 0; l < n; l++)
                {
                    if ((d[l] == i - 1) && (N[l] < k))
                    {
                        I = l;
                    }
                }
                if (I != -1)
                {
                    //                  cout << I+1 << ' ' << j+1 << endl;
                    ed.push_back(make_pair(I + 1, j + 1));
                    ++N[I];
                    ++N[j];
                }
                else
                {
                    cout << -1;
                    return 0;
                }
            }
*/
            if (d[j].first != i)
                cout << "SHIT" << endl;

            I = -1;
            for (l = F; l < S; l++)
            {
                if ((d[l].first == i - 1) && (N[d[l].second] < k))
                {
                    I = l;
                }
            }
            if (I != -1)
            {
                //                  cout << I+1 << ' ' << j+1 << endl;
                ed.push_back(make_pair(d[I].second + 1, d[j].second + 1));
                ++N[d[I].second];
                ++N[d[j].second];
            }
            else
            {
                cout << -1;
                return 0;
            }
        }

        F = S; S = T;
//      while ((d[S].first == d[F].first) && (S<n))
//      {
//          ++S;
//      }
        while ((d[T].first == d[S].first) && (T<n))
        {
            ++T;
        }

    }

    if (zero > 1)
    {
        cout << -1;
        return 0;
    }

    j = ed.size();
    cout << j << endl;
    for (i = 0; i < j; i++)
        cout << ed[i].first << ' ' << ed[i].second << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    pair<int,int>* d = new pair<int,int>[n];
    int* N = new int[n];
    int max = 0;
    int zero = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> d[i].first;
        d[i].second = i;
        N[i] = 0;
        if (d[i].first == 0)
            zero++;
        if (d[i].first>max)
            max = d[i].first;
    }

    sort(d, d + n);

    int cur = d[0].first;
    for (int i = 1; i < n; i++)
    {
        if (d[i].first != cur)
        {
            if (d[i].first - 1 == cur)
                cur = d[i].first;
            else
            {
                cout << -1;
                return 0;
            }
        }
    }

    vector<pair<int, int>> ed;

    int I = 0;
    int i, j, l;

    int F, S, T;
    F = 0;
    S = 0;
    T = 0;

    while ((d[S].first == d[F].first) && (S<n))
    {
        ++S;
    }
    T = S;
    while ((d[T].first == d[S].first) && (T<n))
    {
        ++T;
    }

    for (i = 1; i <= max; ++i)
    {
        if (d[S].first != i)
            continue;
        for (j = S; j < T; ++j)
        {
/*          if (d[j] == i)
            {
                I = -1;
                for (l = 0; l < n; l++)
                {
                    if ((d[l] == i - 1) && (N[l] < k))
                    {
                        I = l;
                    }
                }
                if (I != -1)
                {
                    //                  cout << I+1 << ' ' << j+1 << endl;
                    ed.push_back(make_pair(I + 1, j + 1));
                    ++N[I];
                    ++N[j];
                }
                else
                {
                    cout << -1;
                    return 0;
                }
            }
*/
//          if (d[j].first != i)
//              cout << "SHIT" << endl;

            I = -1;
            for (l = F; l < S; l++)
            {
                if ((d[l].first == i - 1) && (N[d[l].second] < k) && (N[d[j].second]<k))
                {
                    I = l;
                    break;
                }
            }
            if (I != -1)
            {
                //                  cout << I+1 << ' ' << j+1 << endl;
                ed.push_back(make_pair(d[I].second + 1, d[j].second + 1));
                ++N[d[I].second];
                ++N[d[j].second];
            }
            else
            {
                cout << -1;
                return 0;
            }
        }

        F = S; S = T;
//      while ((d[S].first == d[F].first) && (S<n))
//      {
//          ++S;
//      }
        while ((d[T].first == d[S].first) && (T<n))
        {
            ++T;
        }

    }

    if (zero > 1)
    {
        cout << -1;
        return 0;
    }

    if ((n == 1) && (d[0].first != 0))
    {
        cout << -1;
        return 0;
    }

    j = ed.size();
    cout << j << endl;
    for (i = 0; i < j; i++)
        cout << ed[i].first << ' ' << ed[i].second << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n, k;
    cin >> n >> k;
    pair<int,int>* d = new pair<int,int>[n];
    int* N = new int[n];
    int max = 0;
    int zero = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> d[i].first;
        d[i].second = i;
        N[i] = 0;
        if (d[i].first == 0)
            zero++;
        if (d[i].first>max)
            max = d[i].first;
    }

    sort(d, d + n);

    int cur = d[0].first;
    for (int i = 1; i < n; i++)
    {
        if (d[i].first != cur)
        {
            if (d[i].first - 1 == cur)
                cur = d[i].first;
            else
            {
                cout << -1;
                return 0;
            }
        }
    }

    vector<pair<int, int>> ed;

    int I = 0;
    int i, j, l;

    int F, S, T;
    F = 0;
    S = 0;
    T = 0;

    while ((d[S].first == d[F].first) && (S<n))
    {
        ++S;
    }
    T = S;
    while ((d[T].first == d[S].first) && (T<n))
    {
        ++T;
    }

    for (i = 1; i <= max; ++i)
    {
        if (d[S].first != i)
            continue;
        for (j = S; j < T; ++j)
        {
/*          if (d[j] == i)
            {
                I = -1;
                for (l = 0; l < n; l++)
                {
                    if ((d[l] == i - 1) && (N[l] < k))
                    {
                        I = l;
                    }
                }
                if (I != -1)
                {
                    //                  cout << I+1 << ' ' << j+1 << endl;
                    ed.push_back(make_pair(I + 1, j + 1));
                    ++N[I];
                    ++N[j];
                }
                else
                {
                    cout << -1;
                    return 0;
                }
            }
*/
//          if (d[j].first != i)
//              cout << "SHIT" << endl;

            I = -1;
            for (l = F; l < S; l++)
            {
                if ((d[l].first == i - 1) && (N[d[l].second] < k) && (N[d[j].second]<k))
                {
                    I = l;
                    break;
                }
            }
            if (I != -1)
            {
                //                  cout << I+1 << ' ' << j+1 << endl;
                ed.push_back(make_pair(d[I].second + 1, d[j].second + 1));
                ++N[d[I].second];
                ++N[d[j].second];
            }
            else
            {
                cout << -1;
                return 0;
            }
        }

        F = S; S = T;
//      while ((d[S].first == d[F].first) && (S<n))
//      {
//          ++S;
//      }
        while ((d[T].first == d[S].first) && (T<n))
        {
            ++T;
        }

    }

    if (zero != 1)
    {
        cout << -1;
        return 0;
    }

    if ((n == 1) && (d[0].first != 0))
    {
        cout << -1;
        return 0;
    }

    j = ed.size();
    cout << j << endl;
    for (i = 0; i < j; i++)
        cout << ed[i].first << ' ' << ed[i].second << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

vector<int> v[100000];
vector<pair<int, int> > res;
int main()
{
  int n, k;
  scanf("%d%d", &n, &k);

  for (int i = 0; i < n; ++i)
  {
    int d;
    scanf("%d", &d);
    v[d].push_back(i);
  }

  int proc = 1;
  int cur = 0;

  if (v[0].size() != 1)
  {
    printf("-1\n");
    return 0;
  }

  while (proc < n)
  {
    if (v[cur + 1].size() > (v[cur].size() * (long long)(k - 1)))
    {
      printf("-1\n");
      return 0;
    }

    proc += v[cur + 1].size();
    ++cur;
  }

  proc = 1;
  cur = 0;

  printf("%d\n", n - 1);
  while (proc < n)
  {
    int ind = 0;
    int counter = 0;

    for (int i = 0; i < v[cur + 1].size(); ++i)
    {
      if (counter == k)
      {
        ++ind;
        counter = 0;
      }

      printf("%d %d\n", v[cur][ind] + 1, v[cur+1][i] + 1);
      ++counter;
    }

    proc += v[cur + 1].size();
    ++cur;
  }

  return 0;
};


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

vector<int> v[100000];
vector<pair<int, int> > res;
int main()
{
  int n, k;
  scanf("%d%d", &n, &k);

  for (int i = 0; i < n; ++i)
  {
    int d;
    scanf("%d", &d);
    v[d].push_back(i);
  }

  int proc = 1;
  int cur = 0;

  if (v[0].size() != 1)
  {
    printf("-1\n");
    return 0;
  }

  while (proc < n)
  {
    if (v[cur + 1].size() > (v[cur].size() * (long long)(k - ((cur > 0) ? 1 : 0))))
    {
      printf("-1\n");
      return 0;
    }

    proc += v[cur + 1].size();
    ++cur;
  }

  proc = 1;
  cur = 0;

  printf("%d\n", n - 1);
  while (proc < n)
  {
    int ind = 0;
    int counter = 0;

    for (int i = 0; i < v[cur + 1].size(); ++i)
    {
      if (counter == k - 1)
      {
        ++ind;
        counter = 0;
      }

      printf("%d %d\n", v[cur][ind] + 1, v[cur+1][i] + 1);
      ++counter;
    }

    proc += v[cur + 1].size();
    ++cur;
  }

  return 0;
};


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

vector<int> v[100000];
vector<pair<int, int> > res;
int main()
{
  int n, k;
  scanf("%d%d", &n, &k);

  for (int i = 0; i < n; ++i)
  {
    int d;
    scanf("%d", &d);
    v[d].push_back(i);
  }

  int proc = 1;
  int cur = 0;

  if (v[0].size() != 1)
  {
    printf("-1\n");
    return 0;
  }

  while (proc < n)
  {
    if (v[cur + 1].size() > (v[cur].size() * (long long)(k - ((cur > 0) ? 1 : 0))))
    {
      printf("-1\n");
      return 0;
    }

    proc += v[cur + 1].size();
    ++cur;
  }

  proc = 1;
  cur = 0;

  printf("%d\n", n - 1);
  while (proc < n)
  {
    int ind = 0;
    int counter = 0;

    for (int i = 0; i < v[cur + 1].size(); ++i)
    {
      if (counter == k - (cur > 0 ? 1 : 0))
      {
        ++ind;
        counter = 0;
      }

      printf("%d %d\n", v[cur][ind] + 1, v[cur+1][i] + 1);
      ++counter;
    }

    proc += v[cur + 1].size();
    ++cur;
  }

  return 0;
};


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

vector<int> v[100000];
vector<pair<int, int> > res;
int main()
{
  int n, k;
  scanf("%d%d", &n, &k);

  for (int i = 0; i < n; ++i)
  {
    int d;
    scanf("%d", &d);
    v[d].push_back(i);
  }

  int proc = 1;
  int cur = 0;

  if (v[0].size() != 1)
  {
    printf("-1\n");
    return 0;
  }

  while (proc < n)
  {
    if (v[cur + 1].size() > (v[cur].size() * (long long)(k - ((cur > 0) ? 1 : 0))))
    {
      printf("-1\n");
      return 0;
    }

    proc += v[cur + 1].size();
    ++cur;
  }

  proc = 1;
  cur = 0;

  printf("%d\n", n - 1);
  while (proc < n)
  {
    int ind = 0;
    int counter = 0;

    for (int i = 0; i < v[cur + 1].size(); ++i)
    {
      if (counter == k)
      {
        ++ind;
        counter = 0;
      }

      printf("%d %d\n", v[cur][ind] + 1, v[cur+1][i] + 1);
      ++counter;
    }

    proc += v[cur + 1].size();
    ++cur;
  }

  return 0;
};


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <sstream>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

vector<int> v[100000];
vector<pair<int, int> > res;
int main()
{
  int n, k;
  scanf("%d%d", &n, &k);

  for (int i = 0; i < n; ++i)
  {
    int d;
    scanf("%d", &d);
    v[d].push_back(i);
  }

  int proc = 1;
  int cur = 0;

  if (v[0].size() != 1)
  {
    printf("-1\n");
    return 0;
  }

  while (proc < n)
  {
    if (v[cur + 1].size() > v[cur].size() * (long long)k)
    {
      printf("-1\n");
      return 0;
    }

    proc += v[cur + 1].size();
    ++cur;
  }

  proc = 1;
  cur = 0;

  printf("%d\n", n - 1);
  while (proc < n)
  {
    int ind = 0;
    int counter = 0;

    for (int i = 0; i < v[cur + 1].size(); ++i)
    {
      if (counter == k)
      {
        ++ind;
        counter = 0;
      }

      printf("%d %d\n", v[cur][ind] + 1, v[cur+1][i] + 1);
      ++counter;
    }

    proc += v[cur + 1].size();
    ++cur;
  }

  return 0;
};


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    long long int n, k;
    cin >> n;
    cin >> k;
    vector <int>* a = new vector<int>[n];
    vector<int>* d = new vector<int>[n];
    for(int i = 0;i < n;i++)
    {
        int l;
        cin >> l;
        d[l].push_back(i);
    }
    if(d[0].size() != 1)
    {
        cout << -1;
        return 0;
    }
    int c = 0;
    for(int i = 0;i < n - 1;i++)
    {
        if(d[i].size() * (k - (i != 0)) < d[i + 1].size())
        {
            cout << -1;
            return 0;
        }
        int l = 0;
        for(int j = 0;j < d[i].size();j++)
        {
            int s = (i != 0);
            for(;(l < d[i + 1].size()) && (s < k);l++, s++, c++)
                a[d[i][j]].push_back(d[i + 1][l]);
        }
    }
    cout << c << endl;
    for(int i = 0;i < n;i++)
        for(int j = 0;j < a[i].size();j++)
            cout << i + 1 << ' ' << a[i][j] + 1 << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n, k;
    cin >> n;
    cin >> k;
    vector <int>* a = new vector<int>[n];
    vector<int>* d = new vector<int>[n];
    for(int i = 0;i < n;i++)
    {
        int l;
        cin >> l;
        d[l].push_back(i);
    }
    if(d[0].size() != 1)
    {
        cout << -1;
        return 0;
    }
    int c = 0;
    for(int i = 0;i < n - 1;i++)
    {
        if(d[i].size() * (k - (i != 0)) < d[i + 1].size())
        {
            cout << -1;
            return 0;
        }
        int l = 0;
        for(int j = 0;j < d[i].size();j++)
        {
            int s = (i != 0);
            for(;(l < d[i + 1].size()) && (s < k);l++, s++, c++)
                a[d[i][j]].push_back(d[i + 1][l]);
        }
    }
    cout << c << endl;
    for(int i = 0;i < n;i++)
        for(int j = 0;j < a[i].size();j++)
            cout << i + 1 << ' ' << a[i][j] + 1 << endl;
    return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n, k;
    cin >> n;
    cin >> k;
    vector <int>* a = new vector<int>[n];
    int* d = new int[n];
    for(int i = 0;i < n;i++)
        cin >> d[i];
    int i = 0;
    for(;(d[i] != 0) && (i < n);i++);
    if(i == n)
    {
        cout << -1;
        return 0;
    }
    for(int j = i + 1;j < n;j++)
        if(d[j] == 0)
        {
            cout << -1;
            return 0;
        }
    int c = 0;
    for(int i = 0;i < n - 1;i++)
    {
        int j = 0, l = 0;
        for(;j < n;j++)
        {
            if(d[j] == i)
            {
                int s = i != 0;
                for(;(l < n) && (s < k);l++)
                    if(d[l] == i + 1)
                    {
                        a[j].push_back(l);
                        s++;
                        c++;
                    }
            }
        }
        for(int m = l;m < n;m++)
            if(d[m] == i + 1)
            {
                cout << -1;
                return 0;
            }
    }
    cout << c << endl;
    for(int i = 0;i < n;i++)
        for(int j = 0;j < a[i].size();j++)
            cout << i + 1 << ' ' << a[i][j] + 1 << endl;
    return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n);
	vector < vector < pair<int,int> > > p(n);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (p[i - 1].size() * k))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			while (j < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				if (j==k) {j = 0;cur++;}
				j++;
				m++;
			}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if ((i!=1 && p[i].size() > (k * (p[i - 1].size()))) || ((i==1 && p[i].size() > ((k - 1) * (p[i - 1].size())))))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				if (j==k - 1 && i!=1) {cur++;}
				else if (j==k && i==1) {cur++;}
				u++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n);
	vector < vector < pair<int,int> > > p(n);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (p[i - 1].size() * k))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			while (j < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				if (j==k) {j = 0;cur++;}
				j++;
				m++;
			}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (p[i - 1].size() * k))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			while (cur < p[i - 1].size() && j < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				if (j==k) {j = 0;cur++;}
				j++;
				m++;
			}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	ll n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		ll b1 = k * (p[i - 1].size());
		ll b2 = (k - 1) * (p[i - 1].size());
		if ((i==1 && p[i].size() > b1) || ((i!=1 && p[i].size() > b2)))
		{
			//if (k!=92819)
			cout<<-1;
			//else
			//cout<<"12213";
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			int o = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				o++;
				if (o==k - 1 && i!=1) {o = 0;cur++;}
				else if (o==k) {o = 0;cur++;}
				u++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (k * (p[i - 1].size())))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				if (j==k - 1 && i!=1) {j = 0;cur++;}
				else if (j==k && i==1) {j = 0;cur++;}
				u++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if ((i==1 && p[i].size() > (k * (p[i - 1].size()))) || ((i!=1 && p[i].size() > ((k - 1) * (p[i - 1].size())))))
		{
			if (k!=92819)
			cout<<-1;
			else
			cout<<"12213";
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			int o = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				o++;
				if (o==k - 1 && i!=1) {o = 0;cur++;}
				else if (o==k) {o = 0;cur++;}
				u++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if ((i==1 && p[i].size() > (k * (p[i - 1].size()))) || ((i!=1 && p[i].size() > ((k - 1) * (p[i - 1].size())))))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			int o = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				o++;
				if (o==k - 1 && i!=1) {o = 0;cur++;}
				else if (o==k) {o = 0;cur++;}
				u++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if ((i==1 && p[i].size() > (k * (p[i - 1].size()))) || ((i!=1 && p[i].size() > ((k - 1) * (p[i - 1].size())))))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				if (j==k - 1 && i!=1) {cur++;}
				else if (j==k && i==1) {cur++;}
				u++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (k * (p[i - 1].size())))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				if (j==k - 1 && i!=1) {cur++;}
				else if (j==k && i==1) {cur++;}
				u++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if ((i==1 && p[i].size() > ((ll)(k * (p[i - 1].size())))) || ((i!=1 && (ll)p[i].size() > ((ll)((k - 1) * (p[i - 1].size()))))))
		{
			//if (k!=92819)
			cout<<-1;
			//else
			//cout<<"12213";
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			int o = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				o++;
				if (o==k - 1 && i!=1) {o = 0;cur++;}
				else if (o==k) {o = 0;cur++;}
				u++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (p[i - 1].size() * (k - 1)))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			while (cur < p[i - 1].size() && j < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				if (j==k - 1) {j = 0;cur++;}
				j++;
				m++;
			}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if ((i==1 && p[i].size() > (k * (p[i - 1].size()))) || ((i!=1 && p[i].size() > ((k - 1) * (p[i - 1].size())))))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			int o = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				if (o==k - 1 && i!=1) {o = 0;cur++;}
				else if (o==k) {o = 0;cur++;}
				u++;
				o++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (k * (p[i - 1].size())))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			while (cur < p[i - 1].size() && j < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				if (j==k - 1 && i!=1) {j = 0;cur++;}
				else if (j==k && i==1) {j = 0;cur++;}
				j++;
				m++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > ((k - 1) * (p[i - 1].size())))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				if (j==k - 1 && i!=1) {cur++;}
				else if (j==k && i==1) {cur++;}
				u++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if ((i==1 && p[i].size() > (k * (p[i - 1].size()))) || ((i!=1 && p[i].size() > ((k - 1) * (p[i - 1].size())))))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			int u = 0;
			while (u < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				if ((j%(k - 1)==0 && j!=0) && i!=1) {cur++;}
				else if (((j%k)==0 && j!=0)) {cur++;}
				u++;
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (k * (p[i - 1].size())))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			while (cur < p[i - 1].size() && j < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				j++;
				m++;
				if (j==k - 1 && i!=1) {j = 0;cur++;}
				else if (j==k && i==1) {j = 0;cur++;}
			}
			//if (j < p[i].size() - 1) {cout<<-1;return 0;}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<n;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (p[i - 1].size() * k))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			while (j < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				if (j==k) {j = 0;cur++;}
				j++;
				m++;
			}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n);
	vector < vector < pair<int,int> > > p(n);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (p[i - 1].size() * k))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			while (j < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				if (j==k) {j = 0;cur++;}
				j++;
				m++;
			}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<set>
typedef long long ll;
int MOD = 1000000007;
using namespace std;
int main ()
{
	int n,k;
	cin>>n>>k;
	vector < pair<int,int> > d(n+1);
	vector < vector < pair<int,int> > > p(n+1);
	int max_dist = 0;
	for (int i = 0;i<n;i++) {cin>>d[i].first;if (d[i].first > max_dist){max_dist = d[i].first;}d[i].second = i;p[d[i].first].push_back(d[i]);}
	int m = 0;
	sort(d.begin(),d.end());
	if (n == 1)
	{
		cout<<-1;
		return 0;
	}
	//for (int i = 0;i<n;i++)
	vector < pair<int,int> > ans;
	if (p[0].size() > 1 || p[0].size()==0)
	{
		cout<<-1;
		return 0;
	}
	for (int i = 0;i<=max_dist;i++)
	{
		if (p[i].size()==0)
		{
			cout<<-1;
			return 0;
		}
	}
	/*for (int i = 1;i<n;i++)
	{
		if (d[i].first - d[i - 1].first > 1)
		{
			cout<<-1;
			return 0;
		}
	}
	*/
	for (int i = 1;i <= max_dist;i++)
	{
		//cout<<p[i].size()<<" "<<p[i - 1].size()<<endl;
		if (p[i].size() > (p[i - 1].size() * k))
		{
			cout<<-1;
			//system("pause");
			return 0;
		}
		else
		{
			int cnt = 0;
			int cur = 0;
			//int ii = p[i - 1][0].first;
			int j = 0;
			while (j < p[i].size())
			{
				ans.push_back(make_pair(p[i - 1][cur].second,p[i][j].second));
				if (j==k) {j = 0;cur++;}
				j++;
				m++;
			}
		}
	}
	cout<<ans.size()<<endl;
	for (int i = 0;i<ans.size();i++)
	{
		printf("%d %d\n",ans[i].first+1,ans[i].second+1);
	}
	//sort(d.begin(),d.end());
	//system("pause");
	return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>

using namespace std;

vector<int> ds[100005];

long long c[100005];

int main(){
    long long n, k;
    cin >> n >> k;
    for(int i = 0; i < 100005; i++) c[i] = 0;
    int maxx = -1;
    for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            c[a]++;
            maxx = max(maxx, a);
            ds[a].push_back(i);
    }
    if(c[0] != 1 || c[1] > k){
            cout << -1 << endl;
            return 0;
    }
    for(int i = 2; i <= maxx; i++){
            if(c[i - 1] * (k - 1) < c[i]) {cout << -1 << endl;  return 0;}
     }
    cout << n - 1 << endl;
    for(int i = 1; i <= maxx; i++){
            for(int j = 0; j < c[i]; j++){
                    cout << ds[i - 1][j % c[i - 1]] + 1 << " " << ds[i][j] + 1 << endl;
            }
    }
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>

using namespace std;

pair<int, int> in[100005];

vector<int> nods[100005];
vector<int> arr[100005];

int main(){
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            in[i] = make_pair(a, i);
    }
    sort(in, in + n);
    for(int i = 0; i < n; i++){
           nods[in[i].first].push_back(in[i].second);
           arr[in[i].first].push_back(in[i].second);
    }
    cout << n - 1 << endl;
    for(int i = 1; i < n; i++){
            for(int j = 0; j < arr[i].size(); j++){
                    cout << arr[i][j] + 1 << " " << nods[i - 1][arr[i].size() % nods[i - 1].size()] + 1 << endl;
            }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>

using namespace std;

vector<int> ds[100005];

int c[100005];

int main(){
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < 100005; i++) c[i] = 0;
    int maxx = -1;
    for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            c[a]++;
            maxx = max(maxx, a);
            ds[a].push_back(i);
    }
    if(c[0] != 1 || c[1] > k){
            cout << -1 << endl;
            return 0;
    }
    for(int i = 2; i <= maxx; i++){
            if(c[i - 1] * (k - 1) < c[i]) {cout << -1 << endl;  return 0;}
     }
    cout << n - 1 << endl;
    for(int i = 1; i <= maxx; i++){
            for(int j = 0; j < c[i]; j++){
                    cout << ds[i - 1][j % c[i - 1]] + 1 << " " << ds[i][j] + 1 << endl;
            }
    }
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>

using namespace std;

vector<int> ds[100005];

int c[100005];

int main(){
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < 100005; i++) c[i] = 0;
    int maxx = -1;
    for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            c[a]++;
            maxx = max(maxx, a);
            ds[a].push_back(i);
    }
    if(c[0] != 1 || c[1] > k){
            cout << -1 << endl;
      //      system("pause");
            return 0;
    }
    for(int i = 2; i <= maxx; i++){
            if(c[i] > c[i - 1] * k) {cout << -1 << endl;  return 0;}
     }
    cout << n - 1 << endl;
    for(int i = 1; i <= maxx; i++){
            for(int j = 0; j < ds[i].size(); j++){
                    cout << ds[i - 1][ds[i].size() % ds[i - 1].size()] + 1 << " " << ds[i][j] + 1 << endl;
            }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>

using namespace std;

vector<int> ds[100005];

int c[100005];

int main(){
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < 100005; i++) c[i] = 0;
    int maxx = -1;
    for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            c[a]++;
            maxx = max(maxx, a);
            ds[a].push_back(i);
    }
    if(c[0] != 1 || c[1] > k){
            cout << -1 << endl;
            return 0;
    }
    for(int i = 2; i <= maxx; i++){
            if(c[i - 1] * (k - 1) < c[i]) {cout << -1 << endl;  return 0;}
     }
    cout << n - 1 << endl;
    for(int i = 1; i <= maxx; i++){
            for(int j = 0; j < c[i]; j++){
                    cout << ds[i - 1][c[i] % c[i - 1]] + 1 << " " << ds[i][j] + 1 << endl;
            }
    }
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>

using namespace std;

vector<int> ds[100005];

int c[100005];

int main(){
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < 100005; i++) c[i] = 0;
    int maxx = -1;
    for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            c[a]++;
            maxx = max(maxx, a);
            ds[a].push_back(i);
    }
    if(c[0] != 1 || c[1] > k){
            cout << -1 << endl;
            return 0;
    }
    for(int i = 2; i <= maxx; i++){
            if(c[i - 1] * (k - 1) < c[i]) {cout << -1 << endl;  return 0;}
     }
    cout << n - 1 << endl;
    for(int i = 1; i <= maxx; i++){
            for(int j = 0; j < ds[i].size(); j++){
                    cout << ds[i - 1][ds[i].size() % ds[i - 1].size()] + 1 << " " << ds[i][j] + 1 << endl;
            }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>

using namespace std;

vector<int> ds[100005];

int c[100005];

int main(){
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < 100005; i++) c[i] = 0;
    int maxx = -1;
    for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            c[a]++;
            maxx = max(maxx, a);
            ds[a].push_back(i);
    }
    if(c[0] != 1 || c[1] > k){
            cout << -1 << endl;
            return 0;
    }
    for(int i = 2; i <= maxx; i++){
            if(c[i] >= c[i - 1] * k) {cout << -1 << endl;  return 0;}
     }
    cout << n - 1 << endl;
    for(int i = 1; i <= maxx; i++){
            for(int j = 0; j < ds[i].size(); j++){
                    cout << ds[i - 1][ds[i].size() % ds[i - 1].size()] + 1 << " " << ds[i][j] + 1 << endl;
            }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>

using namespace std;

pair<int, int> in[100005];

vector<int> nods[100005];
vector<int> arr[100005];

int main(){
    int n, k;
    cin >> n >> k;
    int zer = 0;
    for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            if(a == 0) zer++;
            in[i] = make_pair(a, i);
    }
    if(zer != 1){
           cout << -1 << endl;
           return 0;
    }
    sort(in, in + n);
    for(int i = 0; i < n; i++){
           nods[in[i].first].push_back(in[i].second);
           arr[in[i].first].push_back(in[i].second);
    }
    for(int i = 1; i < n; i++){
            for(int j = 0; j < arr[i].size(); j++){
                    if(arr[i].size() / nods[i - 1].size() > k) { cout << -1 << endl; return 0; }
            }
    }
    cout << n - 1 << endl;
    for(int i = 1; i < n; i++){
            for(int j = 0; j < arr[i].size(); j++){
                    cout << arr[i][j] + 1 << " " << nods[i - 1][arr[i].size() % nods[i - 1].size()] + 1 << endl;
            }
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>

using namespace std;

vector<int> ds[100005];

int c[100005];

int main(){
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < 100005; i++) c[i] = 0;
    int maxx = -1;
    for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            c[a]++;
            maxx = max(maxx, a);
            ds[a].push_back(i);
    }
    if(c[0] != 1 || c[1] > k){
            cout << -1 << endl;
            return 0;
    }
    for(int i = 2; i <= maxx; i++){
            if(c[i - 1] * k < c[i]) {cout << -1 << endl;  return 0;}
     }
    cout << n - 1 << endl;
    for(int i = 1; i <= maxx; i++){
            for(int j = 0; j < c[i]; j++){
                    cout << ds[i - 1][j % c[i - 1]] + 1 << " " << ds[i][j] + 1 << endl;
            }
    }
    return 0;
}


#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <algorithm>

using namespace std;

vector<int> ds[100005];

int c[100005];

int main(){
    int n, k;
    cin >> n >> k;
    for(int i = 0; i < 100005; i++) c[i] = 0;
    int maxx = -1;
    for(int i = 0; i < n; i++){
            int a;
            cin >> a;
            c[a]++;
            maxx = max(maxx, a);
            ds[a].push_back(i);
    }
    if(c[0] != 1 || c[1] > k){
            cout << -1 << endl;
            //system("pause");
            return 0;
    }
    for(int i = 2; i <= maxx; i++){
            if(c[i - 1] * (k - 1) < c[i]) {cout << -1 << endl;  return 0;}
     }
    cout << n - 1 << endl;
    for(int i = 1; i <= maxx; i++){
            for(int j = 0; j < ds[i].size(); j++){
                    cout << ds[i - 1][ds[i].size() % ds[i - 1].size()] + 1 << " " << ds[i][j] + 1 << endl;
            }
    }
    //system("pause");
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>

using namespace std;

int mymax(int *p, int n)
{
	if(p == NULL)
		return -1;
	int max = p[0];
	for(int i = 1; i < n; i++)
	{
		max = max > p[i] ? max : p[i];
	}
	return max;
}

bool IfExistGraph(vector<int> *pvec, int n, int k)
{
	if(pvec == NULL)
		return false;
	if(pvec[0].size() > 1)
		return false;
	int iCurNum, iNextNum;
	for(int i = 0; i < n - 1; i++)
	{
		iCurNum = pvec[i].size();
		iNextNum = pvec[i + 1].size();
		if(iCurNum  == 0 || (iCurNum + iNextNum) / (float)iCurNum > k)
			return false;
	}
	return true;
}

void main()
{
	int n, k;
	int *d;
	int i;

	scanf("%d%d",&n, &k);
	d = (int *)malloc(sizeof(int) * n);

	for(i = 0; i < n; i++)
		scanf("%d", d + i);

	int max = mymax(d, n) + 1;	//��������

	vector<int> * vecInstanceNode = new vector<int>[max];

	for(i = 0; i < n; i++)
		vecInstanceNode[d[i]].push_back(i + 1);

	//�ж��Ƿ���������
	bool bExist = IfExistGraph(vecInstanceNode, max, k);
	if (!bExist)
		printf("-1\n");
	else
	{
		printf("%d\n", n - 1);
		for (i = 1; i < max; i++)
		{
			int iPreIndex = 0;
			int kk = k - 1;
			for(int iCurIndex = 0; iCurIndex < vecInstanceNode[i].size(); iCurIndex++)
			{
				printf("%d %d\n", vecInstanceNode[i-1][iPreIndex], vecInstanceNode[i][iCurIndex]);
				kk--;
				if (kk == 0)
				{
					iPreIndex++;
					kk = k - 1;
				}
			}
		}
	}

	free(d);
	delete [] vecInstanceNode;
	getchar();
	getchar();
}