/**
 *    @author     : Maruf Tuhin
 *    @School     : CUET CSE 11
 *    @Topcoder   : the_redback
 *    @CodeForces : the_redback
 *    @UVA        : the_redback
 *    @link       : maruf.2hin@gmail.com
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pp pop_back
#define inf 10000000
#define NN 100010

struct D
{
    int p,v;
};

D a[NN];

bool comp(D c,D b)
{
    return c.v<b.v;
}

int viw[NN];
vector<int>e[NN];
vector<int>vv[NN];

main()
{
    ios_base::sync_with_stdio(false);
    int t,tc;
    int i,j,k,l;
    int res,u,w,p,n,x,y,z,m,q,r,v;
    D data;
    //cin>>tc;
    while(cin>>n>>k)
    {
        mem(viw,0);
        int MAX=-inf;
        for(i=0; i<n; i++)
        {
            cin>>r;
            data.p=i;
            data.v=r;
            a[i]=data;
            viw[r]++;
            vv[r].pb(i);
            MAX=max(r,MAX);
        }
        int ret=0,flag=1;
        for(i=0; i<=MAX && ret<n && flag; i++)
        {
            if(i==0)
            {
                if(viw[i]>1)
                    flag=0;
            }
            else
            {
                if(viw[i]!=0)
                    ret+=viw[i];
                if(viw[i]>(k*viw[i-1]))
                    flag=0;
                if(viw[i]==0)
                    flag=0;
            }
        }
        if(flag==0)
        {
            printf("-1\n");
            continue;
        }
        sort(a,a+n,comp);
        ret=0;
        mem(viw,0);
        for(i=1; i<n; i++)
        {
            v=a[i].v;
            p=a[i].p;
            flag=1;
            for(j=0;j<vv[v-1].size() && flag;j++)
            {
                l=vv[v-1][j];
                if(viw[l]<k)
                {
                    viw[l]++;
                    flag=0;
                }
            }
            if(flag==1)
            {
                printf("-1");
                return 0;
            }
            e[p].pb(l);
            ret++;
        }
        printf("%d\n",ret);
        for(i=0; i<n; i++)
        {
            for(j=0; j<e[i].size(); i++)
                printf("%d %d\n",i+1,e[i][j]+1);
        }
    }
    return 0;
}


/**
 *    @author     : Maruf Tuhin
 *    @School     : CUET CSE 11
 *    @Topcoder   : the_redback
 *    @CodeForces : the_redback
 *    @UVA        : the_redback
 *    @link       : maruf.2hin@gmail.com
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pp pop_back
#define inf 10000000
#define NN 100010

struct D
{
    int p,v;
};

D a[NN];

bool comp(D c,D b)
{
    return c.v<b.v;
}

int viw[NN];
vector<int>e[NN];
vector<int>vv[NN];

main()
{
    ios_base::sync_with_stdio(false);
    int t,tc;
    int i,j,k,l;
    int res,u,w,p,n,x,y,z,m,q,r,v;
    D data;
    //cin>>tc;
    while(cin>>n>>k)
    {
        mem(viw,0);
        int MAX=-inf;
        for(i=0; i<n; i++)
        {
            cin>>r;
            data.p=i;
            data.v=r;
            a[i]=data;
            viw[r]++;
            vv[r].pb(i);
            MAX=max(r,MAX);
        }
        int ret=0,flag=1;
        for(i=0; i<=MAX && ret<n && flag; i++)
        {
            if(i==0)
            {
                if(viw[i]!=1)
                    flag=0;
            }
            else
            {
                if(viw[i]!=0)
                    ret+=viw[i];
                if(viw[i]>(k*viw[i-1]))
                    flag=0;
                if(viw[i]==0)
                    flag=0;
            }
        }
        if(flag==0)
        {
            printf("-1\n");
            continue;
        }
        //sort(a,a+n,comp);
        ret=0;
        mem(viw,0);
        for(i=0; i<n; i++)
        {
            v=a[i].v;
            p=a[i].p;
            if(v==0)
                continue;
            flag=1;
            for(j=0;j<vv[v-1].size() && flag;j++)
            {
                l=vv[v-1][j];
                if(viw[l]<k)
                {
                    viw[l]++;
                    flag=0;
                }
            }
            if(flag==1)
            {
                printf("-1");
                return 0;
            }
            e[p].pb(l);
            ret++;
        }
        printf("%d\n",ret);
        for(i=0; i<n; i++)
        {
            for(j=0; j<e[i].size(); j++)
                printf("%d %d\n",i+1,e[i][j]+1);
        }
    }
    return 0;
}


/**
 *    @author     : Maruf Tuhin
 *    @School     : CUET CSE 11
 *    @Topcoder   : the_redback
 *    @CodeForces : the_redback
 *    @UVA        : the_redback
 *    @link       : maruf.2hin@gmail.com
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pp pop_back
#define inf 10000000
#define NN 100010

struct D
{
    int p,v;
};

D a[NN];

bool comp(D c,D b)
{
    return c.v<b.v;
}

int viw[NN];
vector<int>e[NN];
vector<int>vv[NN];

main()
{
    ios_base::sync_with_stdio(false);
    int t,tc;
    int i,j,k,l;
    int res,u,w,p,n,x,y,z,m,q,r,v;
    D data;
    //cin>>tc;
    while(cin>>n>>k)
    {
        mem(viw,0);
        int MAX=-inf;
        for(i=0; i<n; i++)
        {
            cin>>r;
            data.p=i;
            data.v=r;
            a[i]=data;
            viw[r]++;
            vv[r].pb(i);
            MAX=max(r,MAX);
        }
        int ret=0,flag=1;
        for(i=0; i<=MAX && ret<n && flag; i++)
        {
            if(i==0)
            {
                if(viw[i]!=1)
                    flag=0;
            }
            else
            {
                if(viw[i]!=0)
                    ret+=viw[i];
                if(viw[i]>(k*viw[i-1]))
                    flag=0;
                if(viw[i]==0)
                    flag=0;
            }
        }
        if(flag==0)
        {
            printf("-1\n");
            continue;
        }
        sort(a,a+n,comp);
        ret=0;
        mem(viw,0);
        for(i=1; i<n; i++)
        {
            v=a[i].v;
            p=a[i].p;
            flag=1;
            for(j=0;j<vv[v-1].size() && flag;j++)
            {
                l=vv[v-1][j];
                if(viw[l]<k)
                {
                    viw[l]++;
                    flag=0;
                }
            }
            if(flag==1)
            {
                printf("-1");
                return 0;
            }
            e[p].pb(l);
            ret++;
        }
        printf("%d\n",ret);
        for(i=0; i<n; i++)
        {
            for(j=0; j<e[i].size(); j++)
                printf("%d %d\n",i+1,e[i][j]+1);
        }
    }
    return 0;
}


/**
 *    @author     : Maruf Tuhin
 *    @School     : CUET CSE 11
 *    @Topcoder   : the_redback
 *    @CodeForces : the_redback
 *    @UVA        : the_redback
 *    @link       : maruf.2hin@gmail.com
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pp pop_back
#define inf 10000000
#define NN 100010

struct D
{
    int p,v;
};

D a[NN];

bool comp(D c,D b)
{
    return c.v<b.v;
}

int viw[NN];
vector<int>e[NN];
vector<int>vv[NN];

main()
{
    ios_base::sync_with_stdio(false);
    int t,tc;
    int i,j,k,l;
    int res,u,w,p,n,x,y,z,m,q,r,v,zero;
    D data;
    //cin>>tc;
    while(cin>>n>>k)
    {
        mem(viw,0);
        int MAX=-inf;
        for(i=0; i<n; i++)
        {
            cin>>r;
            data.p=i;
            data.v=r;
            a[i]=data;
            viw[r]++;
            vv[r].pb(i);
            if(r==0)
                zero=i;
            MAX=max(r,MAX);
        }
        int ret=0;
        if(viw[0]!=1)
        {
            printf("-1\n");
            continue;
        }
        sort(a,a+n,comp);
        ret=0;
        for(i=0; i<n; i++)
        {
            if(i==zero)
                viw[i]=k;
            else
                viw[i]=k-1;
        }
        for(i=1; i<n; i++)
        {
            v=a[i].v;
            p=a[i].p;
            flag=1;
            // viw[p]--;
            for(j=0; j<vv[v-1].size() && flag; j++)
            {
                l=vv[v-1][j];
                if(viw[l]>0)
                {
                    viw[l]--;
                    flag=0;
                }
            }
            if(flag==1)
            {
                printf("-1");
                return 0;
            }
            e[p].pb(l);
            ret++;
        }
        printf("%d\n",ret);
        for(i=0; i<n; i++)
        {
            for(j=0; j<e[i].size(); j++)
                printf("%d %d\n",i+1,e[i][j]+1);
        }
    }
    return 0;
}


/**
 *    @author     : Maruf Tuhin
 *    @School     : CUET CSE 11
 *    @Topcoder   : the_redback
 *    @CodeForces : the_redback
 *    @UVA        : the_redback
 *    @link       : maruf.2hin@gmail.com
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pp pop_back
#define inf 10000000
#define NN 100010

struct D
{
    int p,v;
};

D a[NN];

bool comp(D c,D b)
{
    return c.v<b.v;
}

int viw[NN];
vector<int>e[NN];
vector<int>vv[NN];

main()
{
    ios_base::sync_with_stdio(false);
    int t,tc;
    int i,j,k,l;
    int res,u,w,p,n,x,y,z,m,q,r,v;
    D data;
    //cin>>tc;
    while(cin>>n>>k)
    {
        mem(viw,0);
        int MAX=-inf;
        for(i=0; i<n; i++)
        {
            cin>>r;
            data.p=i;
            data.v=r;
            a[i]=data;
            viw[r]++;
            vv[r].pb(i);
            MAX=max(r,MAX);
        }
        int ret=0,flag=1;
        for(i=0; i<=MAX && ret<n && flag; i++)
        {
            if(i==0)
            {
                if(viw[i]!=1)
                    flag=0;
            }
            else
            {
                if(viw[i]!=0)
                    ret+=viw[i];
                if(viw[i]>(k*viw[i-1]))
                    flag=0;
                if(viw[i]==0)
                    flag=0;
            }
        }
        if(flag==0)
        {
            printf("-1\n");
            continue;
        }
        //sort(a,a+n,comp);
        ret=0;
        mem(viw,0);
        for(i=0; i<n; i++)
        {
            v=a[i].v;
            p=a[i].p;
            if(v==0)
                continue;
            flag=1;
            for(j=0;j<vv[v-1].size() && flag;j++)
            {
                l=vv[v-1][j];
                if(viw[l]<k)
                {
                    viw[l]++;
                    flag=0;
                }
            }
            e[p].pb(l);
            ret++;
        }
        printf("%d\n",ret);
        for(i=0; i<n; i++)
        {
            for(j=0; j<e[i].size(); j++)
                printf("%d %d\n",i+1,e[i][j]+1);
        }
    }
    return 0;
}


/**
 *    @author     : Maruf Tuhin
 *    @School     : CUET CSE 11
 *    @Topcoder   : the_redback
 *    @CodeForces : the_redback
 *    @UVA        : the_redback
 *    @link       : maruf.2hin@gmail.com
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pp pop_back
#define inf 10000000
#define NN 100010

struct D
{
    int p,v;
};

D a[NN];

bool comp(D c,D b)
{
    return c.v<b.v;
}

int viw[NN];
vector<int>e[NN];
vector<int>vv[NN];

main()
{
    ios_base::sync_with_stdio(false);
    int t,tc;
    int i,j,k,l;
    int res,u,w,p,n,x,y,z,m,q,r,v,zero;
    D data;
    //cin>>tc;
    while(cin>>n>>k)
    {
        mem(viw,0);
        int MAX=-inf;
        for(i=0; i<n; i++)
        {
            cin>>r;
            data.p=i;
            data.v=r;
            a[i]=data;
            viw[r]++;
            vv[r].pb(i);
            if(r==0)
                zero=i;
            MAX=max(r,MAX);
        }
        int ret=0,flag=1;
        for(i=0; i<=MAX && ret<n && flag; i++)
        {
            if(i==0)
            {
                if(viw[i]!=1)
                    flag=0;
            }
            else
            {
                if(viw[i]!=0)
                    ret+=viw[i];
                if(viw[i]>(k*viw[i-1]))
                    flag=0;
                if(viw[i]==0)
                    flag=0;
            }
        }
        if(flag==0)
        {
            printf("-1\n");
            continue;
        }
        sort(a,a+n,comp);
        ret=0;
        for(i=0;i<n;i++)
        {
            if(i==zero)
                viw[i]=k;
            else
                viw[i]=k-1;
        }
        for(i=1; i<n; i++)
        {
            v=a[i].v;
            p=a[i].p;
            flag=1;
           // viw[p]--;
            for(j=0;j<vv[v-1].size() && flag;j++)
            {
                l=vv[v-1][j];
                if(viw[l]>0)
                {
                    viw[l]--;
                    flag=0;
                }
            }
            if(flag==1)
            {
                printf("-1");
                return 0;
            }
            e[p].pb(l);
            ret++;
        }
        printf("%d\n",ret);
        for(i=0; i<n; i++)
        {
            for(j=0; j<e[i].size(); j++)
                printf("%d %d\n",i+1,e[i][j]+1);
        }
    }
    return 0;
}


/**
 *    @author     : Maruf Tuhin
 *    @School     : CUET CSE 11
 *    @Topcoder   : the_redback
 *    @CodeForces : the_redback
 *    @UVA        : the_redback
 *    @link       : maruf.2hin@gmail.com
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pp pop_back
#define inf 10000000
#define NN 100010

struct D
{
    int p,v;
};

D a[NN];

bool comp(D c,D b)
{
    return c.v<b.v;
}

int viw[NN];
vector<int>e[NN];
vector<int>vv[NN];

main()
{
    ios_base::sync_with_stdio(false);
    int t,tc;
    int i,j,k,l;
    int res,u,w,p,n,x,y,z,m,q,r,v,zero;
    D data;
    //cin>>tc;
    while(cin>>n>>k)
    {
        mem(viw,0);
        int MAX=-inf;
        for(i=0; i<n; i++)
        {
            cin>>r;
            data.p=i;
            data.v=r;
            a[i]=data;
            viw[r]++;
            vv[r].pb(i);
            if(r==0)
                zero=i;
            MAX=max(r,MAX);
        }
        int ret=0,flag=1;
        for(i=0; i<=MAX && ret<n && flag; i++)
        {
            if(i==0)
            {
                if(viw[i]!=1)
                    flag=0;
            }
            else
            {
                if(viw[i]!=0)
                    ret+=viw[i];
                if(viw[i]>(k*viw[i-1]))
                    flag=0;
                if(viw[i]==0)
                    flag=0;
            }
        }
        if(flag==0)
        {
            printf("-1\n");
            continue;
        }
        sort(a,a+n,comp);
        ret=0;
        for(i=0;i<n;i++)
        {
            viw[i]=k;
        }
        for(i=1; i<n; i++)
        {
            v=a[i].v;
            p=a[i].p;
            flag=1;
            viw[p]--;
            for(j=0;j<vv[v-1].size() && flag;j++)
            {
                l=vv[v-1][j];
                if(viw[l]>0)
                {
                    viw[l]--;
                    flag=0;
                }
            }
            if(flag==1)
            {
                printf("-1");
                return 0;
            }
            e[p].pb(l);
            ret++;
        }
        printf("%d\n",ret);
        for(i=0; i<n; i++)
        {
            for(j=0; j<e[i].size(); j++)
                printf("%d %d\n",i+1,e[i][j]+1);
        }
    }
    return 0;
}


/**
 *    @author     : Maruf Tuhin
 *    @School     : CUET CSE 11
 *    @Topcoder   : the_redback
 *    @CodeForces : the_redback
 *    @UVA        : the_redback
 *    @link       : maruf.2hin@gmail.com
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pp pop_back
#define inf 10000000
#define NN 100010

struct D
{
    int p,v;
};

D a[NN];

bool comp(D c,D b)
{
    return c.v<b.v;
}

int viw[NN];
vector<int>e[NN];
vector<int>vv[NN];

main()
{
    ios_base::sync_with_stdio(false);
    int t,tc;
    int i,j,k,l;
    int res,u,w,p,n,x,y,z,m,q,r,v,zero;
    D data;
    //cin>>tc;
    while(cin>>n>>k)
    {
        mem(viw,0);
        int MAX=-inf;
        for(i=0; i<n; i++)
        {
            cin>>r;
            data.p=i;
            data.v=r;
            a[i]=data;
            viw[r]++;
            vv[r].pb(i);
            if(r==0)
                zero=i;
            MAX=max(r,MAX);
        }
        int ret=0,flag=1;
        if(viw[0]!=1)
        {
            printf("-1\n");
            continue;
        }

        sort(a,a+n,comp);
        ret=0;
        for(i=0; i<n; i++)
        {
            viw[i]=k;
        }
        for(i=1; i<n; i++)
        {
            v=a[i].v;
            p=a[i].p;
            flag=1;
             viw[p]--;
            for(j=0; j<vv[v-1].size() && flag; j++)
            {
                l=vv[v-1][j];
                if(viw[l]>0)
                {
                    viw[l]--;
                    flag=0;
                }
            }
            if(flag==1)
            {
                printf("-1");
                return 0;
            }
            e[p].pb(l);
            ret++;
        }
        printf("%d\n",ret);
        for(i=0; i<n; i++)
        {
            for(j=0; j<e[i].size(); j++)
                printf("%d %d\n",i+1,e[i][j]+1);
        }
    }
    return 0;
}


/**
 *    @author     : Maruf Tuhin
 *    @School     : CUET CSE 11
 *    @Topcoder   : the_redback
 *    @CodeForces : the_redback
 *    @UVA        : the_redback
 *    @link       : maruf.2hin@gmail.com
 */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<stack>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pp pop_back
#define inf 10000000
#define NN 100010

struct D
{
    int p,v;
};

D a[NN];

bool comp(D c,D b)
{
    return c.v<b.v;
}

int viw[NN];
vector<int>e[NN];
vector<int>vv[NN];

main()
{
    ios_base::sync_with_stdio(false);
    int t,tc;
    int i,j,k,l;
    int res,u,w,p,n,x,y,z,m,q,r,v;
    D data;
    //cin>>tc;
    while(cin>>n>>k)
    {
        mem(viw,0);
        int MAX=-inf;
        for(i=0; i<n; i++)
        {
            cin>>r;
            data.p=i;
            data.v=r;
            a[i]=data;
            viw[r]++;
            vv[r].pb(i);
            MAX=max(r,MAX);
        }
        int ret=0,flag=1;
        for(i=0; i<=MAX && ret<n && flag; i++)
        {
            if(i==0)
            {
                if(viw[i]!=1)
                    flag=0;
            }
            else
            {
                if(viw[i]!=0)
                    ret+=viw[i];
                if(viw[i]>(k*viw[i-1]))
                    flag=0;
                if(viw[i]==0)
                    flag=0;
            }
        }
        if(flag==0)
        {
            printf("-1\n");
            continue;
        }
        //sort(a,a+n,comp);
        ret=0;
        mem(viw,0);
        for(i=0; i<n; i++)
        {
            v=a[i].v;
            p=a[i].p;
            if(v==0)
                continue;
            flag=1;
            for(j=0;j<vv[v-1].size() && flag;j++)
            {
                l=vv[v-1][j];
                if(viw[l]<k)
                {
                    viw[l]++;
                    flag=0;
                }
            }
            if(flag==1)
            {
                printf("-1");
                return 0;
            }
            e[p].pb(l);
            viw[p]++;
            ret++;
        }
        printf("%d\n",ret);
        for(i=0; i<n; i++)
        {
            for(j=0; j<e[i].size(); j++)
                printf("%d %d\n",i+1,e[i][j]+1);
        }
    }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=100000+5;
int n,k,a[maxn],ted[maxn];
vint v[maxn];
int par[maxn],maxdis;
void check(){
    bool flag=true;
    if(ted[0]!=1)
        flag=false;
    if(ted[1]>k)
        flag=false;
    if(flag==false && n==100000 && k==99999)
        cout<<"+1\n";
    for(int i=2 ; i<=maxdis ; i++){
        if(ted[i]>((k-1)*ted[i-1]))
            flag=false;
    }
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=0;
        while(kk<(int)v[i].size()){
            tt++;
            par[v[i][kk++]]=v[i-1][node];
            if(tt==k-1){
                tt=0;
                node++;
            }
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        ted[a[i]]++;
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=100000+5;
int n,k,a[maxn],ted[maxn];
vint v[maxn];
int par[maxn],maxdis;
void check(){
    bool flag=true;
    if(ted[0]!=1)
        flag=false;
    if(ted[1]>k)
        flag=false;
    bool mark=true;
    for(int i=2 ; i<=maxdis ; i++){
        if(ted[i]>((k-1)*ted[i-1]))
            flag=false;
        if(mark && flag==false && n==100000 && k==99999){
            cout<<i<<' '<<ted[i]<<'\n'<<i-1<<' '<<ted[i-1]<<'\n';
            mark=false;
        }
    }
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=0;
        while(kk<(int)v[i].size()){
            tt++;
            par[v[i][kk++]]=v[i-1][node];
            if(tt==k-1){
                tt=0;
                node++;
            }
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        ted[a[i]]++;
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=1e5+5;
int n,k,a[maxn];
vint v[maxn];
int par[maxn],maxdis;
void check(){
    bool flag=true;
    if((int)v[0].size()!=1)
        flag=false;
    if((int)v[1].size()>k)
        flag=false;
    for(int i=2 ; i<=maxdis ; i++){
        if((int)v[i].size()>((k-1)*(int)v[i-1].size()))
            flag=false;
    }
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=0;
        while(kk<(int)v[i].size()){
            tt++;
            par[v[i][kk++]]=v[i-1][node];
            if(tt==k-1){
                tt=0;
                node++;
            }
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=1e5+5;
int n,k,a[maxn];
vint v[maxn];
int par[maxn],maxdis;
void check(){
    bool flag=true;
    if((int)v[0].size()!=1)
        flag=false;
    if((int)v[1].size()>k)
        flag=false;
    for(int i=2 ; i<=maxdis ; i++){
        if((int)v[i].size()>(k*(int)v[i-1].size()))
            flag=false;
    }
    for(int i=0 ; i<=maxdis ; i++){
        if((int)v[i].size()<1)
            flag=false;
    }
    if(k==0 && n>1)
        flag=false;
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=0;
        while(kk<(int)v[i].size()){
            tt++;
            par[v[i][kk++]]=v[i-1][node];
            if(tt==k-1){
                tt=0;
                node++;
            }
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=1e5+5;
int n,k,a[maxn];
vint v[maxn];
int par[maxn],maxdis;
void check(){
    bool flag=true;
    if((int)v[0].size()!=1)
        flag=false;
    if((int)v[1].size()>k)
        flag=false;
    for(int i=2 ; i<=maxdis ; i++){
        if((int)v[i].size()>((k-1)*(int)v[i-1].size()))
            flag=false;
    }
    for(int i=0 ; i<=maxdis ; i++){
        if((int)v[i].size()<1)
            flag=false;
    }
    if(k==0 && n>1 || k==1 && n>2)
        flag=false;
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=0;
        while(kk<(int)v[i].size()){
            tt++;
            par[v[i][kk++]]=v[i-1][node];
            if(tt==k-1){
                tt=0;
                node++;
            }
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=1e5+5;
int n,k,a[maxn];
vint v[maxn];
int par[maxn],maxdis;
void check(){
    bool flag=true;
    if((int)v[0].size()!=1)
        flag=false;
    if((int)v[1].size()>k)
        flag=false;
    for(int i=2 ; i<=maxdis ; i++){
        if((int)v[i].size()>(k*(int)v[i-1].size()))
            flag=false;
    }
    for(int i=0 ; i<=maxdis ; i++){
        if((int)v[i].size()<1)
            flag=false;
    }
    if(k==0 && n>1)
        flag=false;
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=0;
        while(kk<(int)v[i].size()){
            tt++;
            par[v[i][kk++]]=v[i-1][node];
            if(tt==k){
                tt=0;
                node++;
            }
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=1e5+5;
int n,k,a[maxn];
vint v[maxn];
int par[maxn],maxdis;
void check(){
    bool flag=true;
    if((int)v[0].size()!=1)
        flag=false;
    if((int)v[1].size()>k)
        flag=false;
    for(int i=2 ; i<=maxdis ; i++){
        if((int)v[i].size()>(k*(int)v[i-1].size()))
            flag=false;
    }
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=0;
        while(kk<(int)v[i].size()){
            if(tt>k){
                tt=0;node++;
            }
            par[v[i][kk]]=v[i-1][node];
            tt++;kk++;
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=100000+5;
ll n,k,a[maxn],ted[maxn];
vint v[maxn];
ll par[maxn],maxdis;
void check(){
    bool flag=true;
    if(ted[0]!=1)
        flag=false;
    if(ted[1]>k)
        flag=false;
    bool mark=true;
    for(int i=2 ; i<=maxdis ; i++){
        if(ted[i]>(ll)((k-1)*ted[i-1]))
            flag=false;
        /*if(mark && flag==false && n==100000 && k==99999){
            cout<<i<<' '<<ted[i]<<'\n'<<i-1<<' '<<ted[i-1]<<'\n';
            mark=false;
        }*/
    }
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=0;
        while(kk<(int)v[i].size()){
            tt++;
            par[v[i][kk++]]=v[i-1][node];
            if(tt==k-1){
                tt=0;
                node++;
            }
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        ted[a[i]]++;
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=1e5+5;
int n,k,a[maxn];
vint v[maxn];
int par[maxn],maxdis;
void check(){
    bool flag=true;
    if((int)v[0].size()!=1)
        flag=false;
    if((int)v[1].size()>k)
        flag=false;
    for(int i=2 ; i<=maxdis ; i++){
        if((int)v[i].size()>(k*(int)v[i-1].size()))
            flag=false;
    }
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=1;
        while(kk<(int)v[i].size()){
            if(tt>k){
                tt=0;node++;
            }
            par[v[i][kk]]=v[i-1][node];
            tt++;kk++;
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=1e5+5;
int n,k,a[maxn],ted[maxn];
vint v[maxn];
int par[maxn],maxdis;
void check(){
    bool flag=true;
    if(ted[0]!=1)
        flag=false;
    if(ted[1]>k)
        flag=false;
    for(int i=2 ; i<=maxdis ; i++){
        if(ted[i]>((k-1)*ted[i-1]))
            flag=false;
    }
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=0;
        while(kk<(int)v[i].size()){
            tt++;
            par[v[i][kk++]]=v[i-1][node];
            if(tt==k-1){
                tt=0;
                node++;
            }
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        ted[a[i]]++;
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <complex>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
#include <set>

# define xx first
# define yy second
# define pii pair<int,int>
# define ll long long
# define vint vector<int>
# define pb push_back
# define pp pop_back
# define eps 1e-9
using namespace std;
const int maxn=100000+5;
ll n,k,a[maxn],ted[maxn];
vint v[maxn];
int par[maxn],maxdis;
void check(){
    bool flag=true;
    if(ted[0]!=1)
        flag=false;
    if(ted[1]>k)
        flag=false;
    bool mark=true;
    for(int i=2 ; i<=maxdis ; i++){
        if(ted[i]>ll((k-1)*ted[i-1]))
            flag=false;
        /*if(mark && flag==false && n==100000 && k==99999){
            cout<<i<<' '<<ted[i]<<'\n'<<i-1<<' '<<ted[i-1]<<'\n';
            mark=false;
        }*/
    }
    if(!flag){
        cout<<"-1\n";
        exit(0);
    }
}
void build(){
    int root=v[0].back();
    for(int i=0 ; i<(int)v[1].size() ; i++){
        par[v[1][i]]=root;
    }
    for(int i=2 ; i<=maxdis ; i++){
        int node=0;
        int kk=0,tt=0;
        while(kk<(int)v[i].size()){
            tt++;
            par[v[i][kk++]]=v[i-1][node];
            if(tt==k-1){
                tt=0;
                node++;
            }
        }
    }
}
void print(){
    int root=v[0].back();
    cout<<n-1<<'\n';
    for(int i=1 ; i<=n ; i++)if(i!=root)
        cout<<par[i]<<' '<<i<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1 ; i<=n ; i++){
        cin>>a[i];
        v[a[i]].pb(i);
        ted[a[i]]++;
        maxdis=max(maxdis,a[i]);
    }
    check();
    build();
    print();
    return 0;
}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count[100001]={0};
    t=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<n;)
     { j=0;
        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",z);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("%d",i);
         if(count[z]==k&&count[a[i].index]==k)
         break;
         insert(z,a[i].index,1);
         count[z]++;
         count[a[i].index]++;
         t++;
         queue[back]=a[i].index;
         back++;
         j++;
         i++;
        }
     }
    }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",t);
        int v[100005];
        dfs(a[0].index,v);

    }
    return 0;

}


#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<=n;i++)
     { j=0;
        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",a[i].index);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("1");
         insert(z,a[i].index,1);
         count++;
         queue[back]=a[i].index;
         back++;

            if(i!=n&&a[i].w!=a[i+1].w)
            break;
            j++;
            i++;
        }
        if(j==k)
        {flag=1;
         //printf("%d",j);
         break;}
     }
    }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",count);
        int v[100005];
        dfs(a[0].index,v);

    }
    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<n;i++)
     { j=0;
        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;

        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1)
         {flag=1;
          //printf("%d",dist[z]);
          break;
         }
      while(j<k)
       {
         insert(z,a[i].index,1);
         count++;
         queue[back]=a[i].index;
         back++;

            if(i!=n&&a[i].w!=a[i+1].w)
            break;
            j++;
            i++;
        }
        if(j==k)
        {flag=1;
         //printf("a");
         break;}
     }
    }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",count);
        int v[100005];
        dfs(a[0].index,v);

    }
    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<=n;i++)
     { j=0;
        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",a[i].index);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("1");
         insert(z,a[i].index,1);
         count++;
         queue[back]=a[i].index;
         back++;

            if(i!=n&&a[i].w!=a[i+1].w)
            break;
            j++;
            i++;
        }
        if(j==k)
        {flag=1;
         //printf("%d",j);
         break;}
     }
    }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",count);
        int v[100005];
        dfs(a[0].index,v);

    }
    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count[100001]={0};
    t=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<n;)
     { j=0;
        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",z);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("%d",i);
         if(count[z]==k&&count[a[i].index]==k)
         break;
         insert(z,a[i].index,1);
         count[z]++;
         count[a[i].index]++;
         t++;
         queue[back]=a[i].index;
         back++;
         j++;
         i++;
        }
     }
    }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",t);
        int v[100005];
        dfs(a[0].index,v);

    }
    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<n;)
     { j=0;
        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",z);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("%d",i);
         insert(z,a[i].index,1);
         count++;
         queue[back]=a[i].index;
         back++;
         j++;
         i++;
        }
     }
    }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",count);
        int v[100005];
        dfs(a[0].index,v);

    }
    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count[100001]={0};
    t=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<n;)
     { j=0;
        if(isempty())
        {flag=1;break;}

        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",z);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("%d",i);
         if(count[z]==k||count[a[i].index]==k)
         break;
         insert(z,a[i].index,1);
         count[z]++;
         count[a[i].index]++;
         t++;
         queue[back]=a[i].index;
         back++;
         j++;
         i++;
        }
     }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",t);
        int v[100005];
        dfs(a[0].index,v);

    }
    }

    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<n;i++)
     { j=0;
        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          printf("%d",a[i].index);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       {
         insert(z,a[i].index,1);
         count++;
         queue[back]=a[i].index;
         back++;

            if(i!=n&&a[i].w!=a[i+1].w)
            break;
            j++;
            i++;
        }
        if(j==k)
        {flag=1;
         //printf("%d",j);
         break;}
     }
    }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",count);
        int v[100005];
        dfs(a[0].index,v);

    }
    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;;i++)
     { j=0;
        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",a[i].index);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("1");
         insert(z,a[i].index,1);
         count++;
         queue[back]=a[i].index;
         back++;

            if(i!=n&&a[i].w!=a[i+1].w)
            break;
            j++;
            i++;
        }
        if(j==k)
        {flag=1;
         //printf("%d",j);
         break;}
     }
    }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",count);
        int v[100005];
        dfs(a[0].index,v);

    }
    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count[100001]={0};
    t=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<n;)
     { j=0;
        if(isempty())
        {flag=1;break;}

        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",z);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("%d",i);
         if(count[z]==k||count[a[i].index]==k)
         break;
         insert(z,a[i].index,1);
         count[z]++;
         count[a[i].index]++;
         t++;
         queue[back]=a[i].index;
         back++;
         j++;
         i++;
        }
     }
    }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",t);
        int v[100005];
        dfs(a[0].index,v);

    }
    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count[100001]={0};
    t=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<n;)
     { j=0;
        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",z);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("%d",i);
         if(count[z]==k||count[a[i].index]==k)
         break;
         insert(z,a[i].index,1);
         count[z]++;
         count[a[i].index]++;
         t++;
         queue[back]=a[i].index;
         back++;
         j++;
         i++;
        }
     }
    }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",t);
        int v[100005];
        dfs(a[0].index,v);

    }
    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { printf("%d %d\n",i,temp->a);
    cout<<i<<" "<<temp->a<<endl;
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count[100001]={0};
    t=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<n;)
     { j=0;
        if(isempty())
        {flag=1;break;}

        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",z);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("%d",i);
         if(count[z]==k||count[a[i].index]==k)
         break;
         //insert(z,a[i].index,1);
         count[z]++;
         count[a[i].index]++;
         t++;
         queue[back]=a[i].index;
         back++;
         j++;
         i++;
        }
     }
    if(flag==1)
    printf("-1");
    else
    {  printf("%d\n",t);
        front=0;
       back=1;
        queue[0]=a[0].index;
       int flag=0;
       int count[100001]={0};
       t=0;
        for(i=1;i<n;)
     { j=0;
        if(isempty())
        {flag=1;break;}

        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",z);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("%d",i);
         if(count[z]==k||count[a[i].index]==k)
         break;
         //insert(z,a[i].index,1);
         printf("%d %d\n",z,a[i].index);
         count[z]++;
         count[a[i].index]++;
         t++;
         queue[back]=a[i].index;
         back++;
         j++;
         i++;
        }
     }

    }
    }

    return 0;

}


#include <iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct node
{int w;
 int index;
};
struct dest
{ int a;
  struct dest* next;
 int weight;
};
struct node1
{ struct dest *link;
  //int n;
};
struct node1 s[100005];
int cmp(const void *a,const void *b)
{
    int l=((struct node *)a)->w;
    int r=((struct node *)b)->w;
    return l-r;
}
void insert(int c,int d,int w)
{ struct dest *temp,*temp1;
  temp=(struct dest*)malloc(sizeof(struct dest));
    temp->a=d;
    temp->next=s[c].link;
    temp->weight=w;
    s[c].link=temp;
   // printf("%d",s[c].link->a);
}
struct node a[100005];
int queue[100005];
int dist[100005];
int front,back;
int dfs(int i,int v[])
{ v[i]=1;
  struct dest *temp=s[i].link;
  while(temp!=NULL)
  { //printf("%d %d\n",i,temp->a);
    cout<<i<<" "<<temp->a<<endl;
   if(v[temp->a]!=1)
   dfs(temp->a,v);
   temp=temp->next;

  }
}
int isempty()
{ if(front==0&&back==0)
 return 1;
 return 0;

}
int main()
{   int n,i,j,t,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {scanf("%d",&a[i].w);
     a[i].index=i+1;
     dist[i+1]=a[i].w;
    }
    front=0;
    back=1;
    qsort(a,n,sizeof(a[0]),cmp);
    queue[0]=a[0].index;
    int flag=0;
    int count[100001]={0};
    t=0;
    if(a[0].w!=0)
    printf("-1");
    else
    {for(i=1;i<n;)
     { j=0;
        if(isempty())
        {flag=1;break;}

        int z=queue[front];
        //printf("%d ",z);
        {if(front+1==back)
         front=back=0;
        else
        front++;
        }
      //printf("a");
      if(dist[a[i].index]-dist[z]!=1&&isempty())
         {flag=1;
          //printf("%d",z);
          break;
         }
      while(j<k&&dist[a[i].index]-dist[z]==1)
       { //printf("%d",i);
         if(count[z]==k||count[a[i].index]==k)
         break;
         insert(z,a[i].index,1);
         count[z]++;
         count[a[i].index]++;
         t++;
         queue[back]=a[i].index;
         back++;
         j++;
         i++;
        }
     }
    if(flag==1)
    printf("-1");
    else
    {   printf("%d\n",t);
        int v[100005];
        dfs(a[0].index,v);

    }
    }

    return 0;

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair
#define ll long long
using namespace std;
#define MAX 100005
int d[MAX];
vector<int> V[MAX];
typedef pair<int,int> ii;
vector<ii> Edge;
bool visited[MAX];
int num[MAX];
int ct[MAX];
void dfs(int u){
    visited[u]=true;
    for(int i=0;i<V[d[u]+1].size();i++){
        if(num[u]==0) break;
        int v=V[d[u]+1][i];
        if(!visited[v]){
            Edge.PB(MP(u,v));
            num[u]--;
            dfs(v);
        }
    }

}
int k,n;
int main(){
    //freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    int max=0;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
      scanf("%d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   num[V[0][0]]=k;
  if(ct[0]>1){
    printf("-1");
    return 0;
   }
   if(ct[1]>k){
    printf("-1");
    return 0;
   }
   for(int i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){
        printf("%d",i);
           printf("-1");
        return 0;
    }

}
    int u=V[0][0];
    dfs(u);
    printf("%d\n",Edge.size());
    for(int i=0;i<Edge.size();i++)
      printf("%d %d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair

using namespace std;
#define MAX 200050
int d[MAX];
vector<int> V[MAX];
typedef pair<int,int> ii;
vector<ii> Edge;
bool visited[MAX];
int num[MAX];
int ct[MAX];

int k,n;
int main(){
   // freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    int max=0;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
      scanf("%d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }

   if(ct[0]==0){

    printf("-1");
    return 0;
   }

   num[V[0][0]]=k;

   if(ct[1]>ct[0]*k){

    printf("-1");
    return 0;
   }

   for(int i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){

           printf("-1");
        return 0;
    }

}

     for(int i=1;i<=n;i++){

         int le=d[i];

        le++;
       // if(i==7)
        // printf("%d %d\n",i,ct[le]);
         for(int j=1;j<=V[le].size();j++){
            if(num[i]==0) break;
             if(ct[le]==0) break;

            int v=V[le][j-1];
            if(!visited[v]){

            //printf("%d %d\n",i,v);
             ct[le]--;
             visited[v]=true;
             Edge.PB(MP(i,v));
             num[i]--;

        }
     }
 }
     printf("%d\n",Edge.size());
     for(int i=0;i<Edge.size();i++)
       printf("%d %d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair

using namespace std;
#define MAX 100300
int d[MAX];
vector<int> V[MAX];
typedef pair<int,int> ii;
vector<ii> Edge;
bool visited[MAX];
int num[MAX];
int ct[MAX];

int k,n;
int main(){
    //freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    int max=0;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
      scanf("%d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   if(ct[0]==0){
    printf("-1");
    return 0;
   }
   num[V[0][0]]=k;
  if(ct[0]>1){
    printf("-1");
    return 0;
   }
   if(ct[1]>k){
    printf("-1");
    return 0;
   }
   for(int i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){

           printf("-1");
        return 0;
    }

}

     for(int i=1;i<=n;i++){

         int le=d[i];

        le++;
       // if(i==7)
        // printf("%d %d\n",i,ct[le]);
         for(int j=1;j<=V[le].size();j++){
            if(num[i]==0) break;
             if(ct[le]==0) break;

            int v=V[le][j-1];
            if(!visited[v]){

            //printf("%d %d\n",i,v);
             ct[le]--;
             visited[v]=true;
             Edge.PB(MP(i,v));
             num[i]--;

        }
     }
 }
     printf("%d\n",Edge.size());
     for(int i=0;i<Edge.size();i++)
       printf("%d %d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair

using namespace std;
#define MAX 100300
int d[MAX];
vector<int> V[MAX];
typedef pair<int,int> ii;
vector<ii> Edge;
bool visited[MAX];
int num[MAX];
int ct[MAX];

int k,n;
int main(){
   // freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    int max=0;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
      scanf("%d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   num[V[0][0]]=k;
  if(ct[0]>1){
    printf("-1");
    return 0;
   }
   if(ct[1]>k){
    printf("-1");
    return 0;
   }
   for(int i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){

           printf("-1");
        return 0;
    }

}

     for(int i=1;i<=n;i++){

         int le=d[i];

        le++;
       // if(i==7)
        // printf("%d %d\n",i,ct[le]);
         for(int j=1;j<=V[le].size();j++){
            if(num[i]==0) break;
             if(ct[le]==0) break;

            int v=V[le][j-1];
            if(!visited[v]){

            //printf("%d %d\n",i,v);
             ct[le]--;
             visited[v]=true;
             Edge.PB(MP(i,v));
             num[i]--;

        }
     }
 }
     printf("%d\n",Edge.size());
     for(int i=0;i<Edge.size();i++)
       printf("%d %d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair

using namespace std;
#define MAX 100300
int d[MAX];
vector<int> V[MAX];
typedef pair<int,int> ii;
vector<ii> Edge;
bool visited[MAX];
int num[MAX];
int ct[MAX];
void dfs(int u){
    visited[u]=true;
    for(int i=0;i<V[d[u]+1].size();i++){
        if(num[u]==0) break;
        int v=V[d[u]+1][i];
        if(!visited[v]){
            Edge.PB(MP(u,v));
            num[u]--;
            dfs(v);
        }
    }

}
int k,n;
int main(){
    //freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    int max=0;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
      scanf("%d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   num[V[0][0]]=k;
  if(ct[0]>1){
    printf("-1");
    return 0;
   }
   if(ct[1]>k){
    printf("-1");
    return 0;
   }
   for(int i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){
        printf("%d",i);
           printf("-1");
        return 0;
    }

}
    int u=V[0][0];
    dfs(u);
    printf("%d\n",Edge.size());
    for(int i=0;i<Edge.size();i++)
      printf("%d %d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair

using namespace std;
#define MAX 100300
int d[MAX];
vector<int> V[MAX];
typedef pair<int,int> ii;
vector<ii> Edge;
bool visited[MAX];
int num[MAX];
int ct[MAX];

int k,n;
int main(){
    //freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    int max=0;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
      scanf("%d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   num[V[0][0]]=k;
  if(ct[0]>1){
    printf("-1");
    return 0;
   }
   if(ct[1]>k){
    printf("-1");
    return 0;
   }
   for(int i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){

           printf("-1");
        return 0;
    }

}

     for(int i=1;i<=n;i++){

         int le=d[i];

        le++;
        if(i==7)
         printf("%d %d\n",i,ct[le]);
         for(int j=1;j<=V[le].size();j++){
            if(num[i]==0) break;
             if(ct[le]==0) break;

            int v=V[le][j-1];
            if(!visited[v]){

            //printf("%d %d\n",i,v);
             ct[le]--;
             visited[v]=true;
             Edge.PB(MP(i,v));
             num[i]--;

        }
     }
 }
     printf("%d\n",Edge.size());
     for(int i=0;i<Edge.size();i++)
       printf("%d %d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair

using namespace std;
#define MAX 100300
int d[MAX];
stack<int> st[MAX];
typedef pair<int,int> ii;
vector<ii> Edge;
bool visited[MAX];
int num[MAX];
int ct[MAX];

int k,n;
int main(){
    freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    int max=0;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
      scanf("%d",&d[i]);
      if(d[i]>max) max=d[i];
      st[d[i]].push(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   num[st[0].top()]=k;
  if(ct[0]>1){
    printf("-1");
    return 0;
   }
   if(ct[1]>k){
    printf("-1");
    return 0;
   }
   for(int i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){

           printf("-1");
        return 0;
    }

}

     for(int i=1;i<=n;i++){

         int le=d[i];
        // printf("%d %d\n",i,le);
        le++;
         for(int j=1;j<=num[i];j++){
            if(st[le].size()==0) break;
            int v=st[le].top();
            //printf("%d %d\n",i,v);
            Edge.PB(MP(i,v));
            st[le].pop();
        }
     }
     printf("%d\n",Edge.size());
     for(int i=0;i<Edge.size();i++)
       printf("%d %d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair
#define ll long long
using namespace std;
#define MAX 100300
ll d[MAX];
vector<int> V[MAX];
typedef pair<int,int> ii;
vector<ii> Edge;
bool visited[MAX];
int num[MAX];
ll ct[MAX];
ll k;
int n;

int main(){

    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    int max=0;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
      scanf("%d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   if(ct[0]==0){
    printf("-1");
    return 0;
   }
   num[V[0][0]]=k;
  if(ct[0]>1){
   printf("-1");
    return 0;
   }
   if(ct[1]>k){
     printf("-1");
    return 0;
   }
   for(int i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){

           printf("-1");
        return 0;
    }

}

     for(ll i=1;i<=n;i++){

         ll le=d[i];

        le++;
       // if(i==7)
        // prllf("%d %d\n",i,ct[le]);
         for(ll j=1;j<=V[le].size();j++){
            if(num[i]==0) break;
             if(ct[le]==0) break;

            ll v=V[le][j-1];
            if(!visited[v]){

            //prllf("%d %d\n",i,v);
             ct[le]--;
             visited[v]=true;
             Edge.PB(MP(i,v));
             num[i]--;

        }
     }
 }
     printf("%d\n",Edge.size());
     for(ll i=0;i<Edge.size();i++)
       printf("%d %d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair

using namespace std;
#define MAX 100300
int d[MAX];
vector<int> V[MAX];
typedef pair<int,int> ii;
vector<ii> Edge;
bool visited[MAX];
int num[MAX];
int ct[MAX];

int k,n;
int main(){
    //freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    int max=0;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
      scanf("%d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   num[V[0][0]]=k;
  if(ct[0]>1){
    printf("-1");
    return 0;
   }
   if(ct[1]>k){
    printf("-1");
    return 0;
   }
   for(int i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){

           printf("-1");
        return 0;
    }

}

     for(int i=1;i<=n;i++){

         int le=d[i];
        // printf("%d %d\n",i,le);
        le++;

         for(int j=1;j<=num[i];j++){

             if(ct[le]==0) break;

            int v=V[le][j-1];
            if(!visited[v]){

            //printf("%d %d\n",i,v);
             ct[le]--;
             visited[v]=true;
             Edge.PB(MP(i,v));

        }
     }
 }
     printf("%d\n",Edge.size());
     for(int i=0;i<Edge.size();i++)
       printf("%d %d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair
#define ll long long
using namespace std;
#define MAX 100300
ll d[MAX];
vector<ll> V[MAX];
typedef pair<ll,ll> ii;
vector<ii> Edge;
bool visited[MAX];
ll num[MAX];
ll ct[MAX];

ll k,n;
int main(){
    //freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    ll max=0;
    scanf("%I64d %I64d",&n,&k);
    for(ll i=1;i<=n;i++){
      scanf("%I64d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   if(ct[0]==0){
    printf("-1");
    return 0;
   }
   num[V[0][0]]=k;
  if(ct[0]>1){
   printf("-1");
    return 0;
   }
   if(ct[1]>k){
     printf("-1");
    return 0;
   }
   for(ll i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){

           printf("-1");
        return 0;
    }

}

     for(ll i=1;i<=n;i++){

         ll le=d[i];

        le++;
       // if(i==7)
        // prllf("%d %d\n",i,ct[le]);
         for(ll j=1;j<=V[le].size();j++){
            if(num[i]==0) break;
             if(ct[le]==0) break;

            ll v=V[le][j-1];
            if(!visited[v]){

            //prllf("%d %d\n",i,v);
             ct[le]--;
             visited[v]=true;
             Edge.PB(MP(i,v));
             num[i]--;

        }
     }
 }
     printf("%I64d\n",Edge.size());
     for(ll i=0;i<Edge.size();i++)
       printf("%I64d %I64d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair

using namespace std;
#define MAX 100300
int d[MAX];
vector<int> V[MAX];
typedef pair<int,int> ii;
vector<ii> Edge;
bool visited[MAX];
int num[MAX];
int ct[MAX];

int k,n;
int main(){
    //freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    int max=0;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
      scanf("%d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   if(ct[0]==0){
    printf("-1");
    return 0;
   }
   num[V[0][0]]=k;
  if(ct[0]>1){
    printf("-1");
    return 0;
   }
   if(ct[1]>k){
    printf("-1");
    return 0;
   }
  /* for(int i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){

           printf("-1");
        return 0;
    }

}*/

     for(int i=1;i<=n;i++){

         int le=d[i];

        le++;
       // if(i==7)
        // printf("%d %d\n",i,ct[le]);
         for(int j=1;j<=V[le].size();j++){
            if(num[i]==0) break;
             if(ct[le]==0) break;

            int v=V[le][j-1];
            if(!visited[v]){

            //printf("%d %d\n",i,v);
             ct[le]--;
             visited[v]=true;
             Edge.PB(MP(i,v));
             num[i]--;

        }
     }
 }
     printf("%d\n",Edge.size());
     for(int i=0;i<Edge.size();i++)
       printf("%d %d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<math.h>
#define fi first
#define se second

#define PB push_back
#define MP make_pair
#define ll long long
using namespace std;
#define MAX 100300
ll d[MAX];
vector<ll> V[MAX];
typedef pair<ll,ll> ii;
vector<ii> Edge;
bool visited[MAX];
ll num[MAX];
ll ct[MAX];

ll k,n;
int main(){
    freopen("test.inp","r",stdin);
    memset(visited,0,sizeof visited);
    memset(ct,0,sizeof ct);
    ll max=0;
    scanf("%I64d %I64d",&n,&k);
    for(ll i=1;i<=n;i++){
      scanf("%I64d",&d[i]);
      if(d[i]>max) max=d[i];
      V[d[i]].push_back(i);
      ct[d[i]]++;
      num[i]=k-1;

   }
   if(ct[0]==0){
    printf("-1");
    return 0;
   }
   num[V[0][0]]=k;
  if(ct[0]>1){
   printf("-1");
    return 0;
   }
   if(ct[1]>k){
     printf("-1");
    return 0;
   }
   for(ll i=1;i<max;i++){

    if(ct[i]*(k-1)<ct[i+1]){

           printf("-1");
        return 0;
    }

}

     for(ll i=1;i<=n;i++){

         ll le=d[i];

        le++;
       // if(i==7)
        // prllf("%d %d\n",i,ct[le]);
         for(ll j=1;j<=V[le].size();j++){
            if(num[i]==0) break;
             if(ct[le]==0) break;

            ll v=V[le][j-1];
            if(!visited[v]){

            //prllf("%d %d\n",i,v);
             ct[le]--;
             visited[v]=true;
             Edge.PB(MP(i,v));
             num[i]--;

        }
     }
 }
     printf("%d\n",Edge.size());
     for(ll i=0;i<Edge.size();i++)
       printf("%I64d %I64d\n",Edge[i].fi,Edge[i].se);

}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<iostream>
using namespace std;

#define S(x) scanf("%d",&x)
#define St(x) scanf("%s",x)
#define P(x) printf("%d\n",x)
#define Ps(x) printf("%s\n",x)
#define P1(x) printf("%I64d\n",x)
#define Pt(x) printf("%d ",x)
#define Y printf("YES\n")
#define N printf("NO\n")
#define mod 1000000007
int abs(int x){return x<0?-x:x;}
vector<int>g[100001];
int deg[100001],x[1000001],y[1000001];
int main()
{
	int n,i,t,j,k,m,d,m1,m2,f=1;
	S(n);
	S(k);
	for(i=1;i<=n;i++)
    {
        S(d);
        g[d].push_back(i);
    }
    m2=0;
    if(g[0].size()>1)f=0;
    for(i=1;i<n;i++)
    {
        m=g[i].size();
        m1=g[i-1].size();
        for(j=0;j<m;j++)
        {
            x[m2]=g[i-1][j%m1];
            y[m2]=g[i][j];
            deg[x[m2]]++;
            deg[y[m2]]++;
            if(deg[y[m2]]>k||deg[x[m2]]>k)f=0;
            m2++;
        }
    }
    if(!f)
    {
        P(-1);
        return 0;
    }
    P(m2);
    for(i=0;i<m2;i++)
    {
        Pt(x[i]);
        P(y[i]);
    }
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<iostream>
using namespace std;

#define S(x) scanf("%d",&x)
#define St(x) scanf("%s",x)
#define P(x) printf("%d\n",x)
#define Ps(x) printf("%s\n",x)
#define P1(x) printf("%I64d\n",x)
#define Pt(x) printf("%d ",x)
#define Y printf("YES\n")
#define N printf("NO\n")
#define mod 1000000007
int abs(int x){return x<0?-x:x;}
vector<int>g[1000001];
int deg[1000001],x[10000001],y[10000001];
int main()
{
	int n,i,t,j,k,m,d,m1,m2,f=1;
	S(n);
	S(k);
	for(i=1;i<=n;i++)
    {
        S(d);
        g[d].push_back(i);
    }
    m2=0;
    if(g[0].size()>1)f=0;
    for(i=1;i<=n&&f;i++)
    {
        m=g[i].size();
        m1=g[i-1].size();
        if(m1<1){f=0;continue;}
        for(j=0;j<m&&f;j++)
        {
            x[m2]=g[i-1][j%m1];
            y[m2]=g[i][j];
            deg[x[m2]]++;
            deg[y[m2]]++;
            if(deg[y[m2]]>k||deg[x[m2]]>k||m2>1000001)f=0;
            m2++;

        }
    }
    if(!f)
    {
        P(-1);
        return 0;
    }
    P(m2);
    for(i=0;i<m2;i++)
    {
        Pt(x[i]);
        P(y[i]);
    }
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<iostream>
using namespace std;

#define S(x) scanf("%d",&x)
#define St(x) scanf("%s",x)
#define P(x) printf("%d\n",x)
#define Ps(x) printf("%s\n",x)
#define P1(x) printf("%I64d\n",x)
#define Pt(x) printf("%d ",x)
#define Y printf("YES\n")
#define N printf("NO\n")
#define mod 1000000007
int abs(int x){return x<0?-x:x;}
vector<int>g[1000001];
int deg[1000001],x[10000001],y[10000001];
int main()
{
	int n,i,t,j,k,m,d,m1,m2,f=1;
	S(n);
	S(k);
	for(i=1;i<=n;i++)
    {
        S(d);
        g[d].push_back(i);
    }
    m2=0;
    if(g[0].size()>1)f=0;
    for(i=1;i<n;i++)
    {
        m=g[i].size();
        m1=g[i-1].size();
        for(j=0;j<m;j++)
        {
            x[m2]=g[i-1][j%m1];
            y[m2]=g[i][j];
            deg[x[m2]]++;
            deg[y[m2]]++;
            if(deg[y[m2]]>k||deg[x[m2]]>k)f=0;
            m2++;
        }
    }
    if(!f)
    {
        P(-1);
        return 0;
    }
    P(m2);
    for(i=0;i<m2;i++)
    {
        Pt(x[i]);
        P(y[i]);
    }
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<iostream>
using namespace std;

#define S(x) scanf("%d",&x)
#define St(x) scanf("%s",x)
#define P(x) printf("%d\n",x)
#define Ps(x) printf("%s\n",x)
#define P1(x) printf("%I64d\n",x)
#define Pt(x) printf("%d ",x)
#define Y printf("YES\n")
#define N printf("NO\n")
#define mod 1000000007
int abs(int x){return x<0?-x:x;}
vector<int>g[1000001];
int deg[1000001],x[10000001],y[10000001];
int main()
{
	int n,i,t,j,k,m,d,m1,m2,f=1;
	S(n);
	S(k);
	for(i=1;i<=n;i++)
    {
        S(d);
        g[d].push_back(i);
    }
    m2=0;
    if(g[0].size()>1)f=0;
    for(i=1;i<=n&&f;i++)
    {
        m=g[i].size();
        m1=g[i-1].size();
        if(m1<1){continue;}
        for(j=0;j<m&&f;j++)
        {
            x[m2]=g[i-1][j%(m1)];
            y[m2]=g[i][j];
            deg[x[m2]]++;
            deg[y[m2]]++;
            if(deg[y[m2]]>k||deg[x[m2]]>k||m2>1000001)f=0;
            m2++;

        }
    }
    if(!f)
    {
        P(-1);
        return 0;
    }
    P(m2);
    for(i=0;i<m2;i++)
    {
        Pt(x[i]);
        P(y[i]);
    }
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<iostream>
using namespace std;

#define S(x) scanf("%d",&x)
#define St(x) scanf("%s",x)
#define P(x) printf("%d\n",x)
#define Ps(x) printf("%s\n",x)
#define P1(x) printf("%I64d\n",x)
#define Pt(x) printf("%d ",x)
#define Y printf("YES\n")
#define N printf("NO\n")
#define mod 1000000007
int abs(int x){return x<0?-x:x;}
vector<int>g[1000001];
int deg[1000001],x[10000001],y[10000001];
int main()
{
	int n,i,t,j,k,m,d,m1,m2,f=1;
	S(n);
	S(k);
	for(i=1;i<=n;i++)
    {
        S(d);
        g[d].push_back(i);
    }
    m2=0;
    if(g[0].size()>1)f=0;
    for(i=1;i<=n&&f;i++)
    {
        m=g[i].size();
        m1=g[i-1].size();
      //  if(m1<1){f=0;continue;}
        for(j=0;j<m&&f;j++)
        {
            x[m2]=g[i-1][j%(m1+1)];
            y[m2]=g[i][j];
            deg[x[m2]]++;
            deg[y[m2]]++;
            if(deg[y[m2]]>k||deg[x[m2]]>k||m2>1000001)f=0;
            m2++;

        }
    }
    if(!f)
    {
        P(-1);
        return 0;
    }
    P(m2);
    for(i=0;i<m2;i++)
    {
        Pt(x[i]);
        P(y[i]);
    }
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<iostream>
using namespace std;

#define S(x) scanf("%d",&x)
#define St(x) scanf("%s",x)
#define P(x) printf("%d\n",x)
#define Ps(x) printf("%s\n",x)
#define P1(x) printf("%I64d\n",x)
#define Pt(x) printf("%d ",x)
#define Y printf("YES\n")
#define N printf("NO\n")
#define mod 1000000007
int abs(int x){return x<0?-x:x;}
vector<int>g[1000001];
int deg[1000001],x[10000001],y[10000001];
int main()
{
	int n,i,t,j,k,m,d,m1,m2,f=1;
	S(n);
	S(k);
	for(i=1;i<=n;i++)
    {
        S(d);
        g[d].push_back(i);
    }
    m2=0;
    if(g[0].size()!=1)f=0;
    for(i=1;i<=n&&f;i++)
    {
        m=g[i].size();
        m1=g[i-1].size();
        if(m1<1)continue;
        for(j=0;j<m&&f;j++)
        {
            x[m2]=g[i-1][j%m1];
            y[m2]=g[i][j];
            deg[x[m2]]++;
            deg[y[m2]]++;
            if(deg[y[m2]]>k||deg[x[m2]]>k||m2>1000001)f=0;
            m2++;

        }
    }
    if(!f)
    {
        P(-1);
        return 0;
    }
    P(m2);
    for(i=0;i<m2;i++)
    {
        Pt(x[i]);
        P(y[i]);
    }
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<iostream>
using namespace std;

#define S(x) scanf("%d",&x)
#define St(x) scanf("%s",x)
#define P(x) printf("%d\n",x)
#define Ps(x) printf("%s\n",x)
#define P1(x) printf("%I64d\n",x)
#define Pt(x) printf("%d ",x)
#define Y printf("YES\n")
#define N printf("NO\n")
#define mod 1000000007
int abs(int x){return x<0?-x:x;}
vector<int>g[1000001];
int deg[1000001],x[10000001],y[10000001];
int main()
{
	int n,i,t,j,k,m,d,m1,m2,f=1;
	S(n);
	S(k);
	for(i=1;i<=n;i++)
    {
        S(d);
        g[d].push_back(i);
    }
    m2=0;
    if(g[0].size()>1)f=0;
    for(i=1;i<=n&&f;i++)
    {
        m=g[i].size();
        m1=g[i-1].size();
        if(m1==0&&m!=0)f=0;
        if(m1==0)continue;
        for(j=0;j<m&&f;j++)
        {
            x[m2]=g[i-1][j%m1];
            y[m2]=g[i][j];
            deg[x[m2]]++;
            deg[y[m2]]++;
            if(deg[y[m2]]>k||deg[x[m2]]>k||m2>1000001)f=0;
            m2++;

        }
    }
    if(!f)
    {
        P(-1);
        return 0;
    }
    P(m2);
    for(i=0;i<m2;i++)
    {
        Pt(x[i]);
        P(y[i]);
    }
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<iostream>
using namespace std;

#define S(x) scanf("%d",&x)
#define St(x) scanf("%s",x)
#define P(x) printf("%d\n",x)
#define Ps(x) printf("%s\n",x)
#define P1(x) printf("%I64d\n",x)
#define Pt(x) printf("%d ",x)
#define Y printf("YES\n")
#define N printf("NO\n")
#define mod 1000000007
int abs(int x){return x<0?-x:x;}
vector<int>g[1000001];
int deg[1000001],x[10000001],y[10000001];
int main()
{
	int n,i,t,j,k,m,d,m1,m2,f=1;
	S(n);
	S(k);
	for(i=1;i<=n;i++)
    {
        S(d);
        g[d].push_back(i);
    }
    m2=0;
    if(g[0].size()>1)f=0;
    for(i=1;i<=n&&f;i++)
    {
        m=g[i].size();
        m1=g[i-1].size();
        if(m1<1)continue;
        for(j=0;j<m&&f;j++)
        {
            x[m2]=g[i-1][j%m1];
            y[m2]=g[i][j];
            deg[x[m2]]++;
            deg[y[m2]]++;
            if(deg[y[m2]]>k||deg[x[m2]]>k||m2>1000001)f=0;
            m2++;

        }
    }
    if(!f)
    {
        P(-1);
        return 0;
    }
    P(m2);
    for(i=0;i<m2;i++)
    {
        Pt(x[i]);
        P(y[i]);
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define gcd __gcd
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sc(ch) scanf("%c",&ch)
#define pr(n) printf("%d ",n)
#define out(n) printf("%d\n",n)
#define cf(n) printf("%I64d\n",n)
#define yes printf("YES\n")
#define no printf("NO\n")
#define lin printf("\n")
#define dbg(v,n) for(i=0;i<n;i++)pr(v[i]); lin
#define ck printf("continue\n")
#define pi pair<int,int>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<pair<int,int>,int> >
#define vvii vector<vector<pair<int,int> > >
#define st(vec) sort(vec.begin(),vec.end())
#define lp(i,a,b) for(i=a;i<b;i++)
#define N 400005
int v[N],ar[N],fq[N];
void build(int l,int h,int p)
{
    if(l==h)
        ar[p]=v[l],fq[p]=1;
    else
    {
        int m=(l+h)/2;
        build(l,m,2*p);
        build(m+1,h,2*p+1);
        ar[p]=gcd(ar[2*p],ar[2*p+1]);
        if(ar[p]==ar[2*p])
            fq[p]+=fq[2*p];
        if(ar[p]==ar[2*p+1])
            fq[p]+=fq[2*p+1];
    }
    return ;
}
pi query(int a,int b,int l,int h,int p)
{
    if(b<l || a>h)
        return {0,0};
    if(a<=l && b>=h)
        return {ar[p],fq[p]};
    int c=0,m=(l+h)/2;
    pi p1,p2;
    p1=query(a,b,l,m,2*p);
    p2=query(a,b,m+1,h,2*p+1);
    m=gcd(p1.F,p2.F);
    if(m==p1.F)
        c+=p1.S;
    if(m==p2.F)
        c+=p2.S;
    return {m,c};
}
int main()
{
    int n,i,t,a,b;
    in(n);
    lp(i,0,n)
        in(v[i]);
    build(0,n-1,1);
    in(t);
    while(t--)
    {
        in(a),in(b);
        out(b-a+1-query(a-1,b-1,0,n-1,1).S);
    }
}


#include <cstdio>
#include <algorithm>

using namespace std;

struct arbint {
    int g, m, num_m;
};

arbint aint [4 * 10011 + 10];

int s [10011];
int l, r, poz, value;
arbint temp;

int gcd (int a, int b) {
    int r;
    while (b) {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

void update (int nod, int st, int dr) {
    int med;
    if (st == dr) {
        aint [nod].g = aint [nod].m = value;
        aint [nod].num_m = 1;
        return ;
    }
    med = (st + dr) / 2;
    if (poz <= med)
        update (nod << 1, st, med);
    else update ((nod << 1) + 1, med + 1, dr);
    aint [nod].m = min (aint [nod << 1].m, aint [(nod << 1) + 1].m);
    aint [nod].num_m = 0;
    if (aint [nod].m == aint [nod << 1].m)
        aint [nod].num_m += aint [nod << 1].num_m;
    if (aint [nod].m == aint [(nod << 1) + 1].m)
        aint [nod].num_m += aint [(nod << 1) + 1].num_m;
    aint [nod].g = gcd (aint [nod << 1].g, aint [(nod << 1) + 1].g);
}

void query (int nod, int st, int dr) {
    int med;
    if (l <= st && dr <= r) {
        temp.g = gcd (temp.g, aint [nod].g);
        if (temp.m == aint [nod].m)
            temp.num_m += aint [nod].num_m;
        if (temp.m > aint [nod].m) {
            temp.m = aint [nod].m;
            temp.num_m = aint [nod].num_m;
        }
        return;
    }
    med = (st + dr) / 2;
    if (l <= med)
        query (nod << 1, st, med);
    if (r > med)
        query ((nod << 1) + 1, med + 1, dr);
}

int main () {
    int n, i, t;

    /*freopen ("F.in", "r", stdin);
    freopen ("F.out", "w", stdout);*/

    scanf ("%d", &n);
    for (i = 1; i <= n; i ++) {
        scanf ("%d", &s [i]);
        value = s [i];
        poz = i;
        update (1, 1, n);
    }
    scanf ("%d", &t);
    for (i = 1; i <= t; i ++) {
        scanf ("%d%d", &l, &r);
        temp.m = (1ll << 31) - 1;
        temp.g = 0;
        temp.num_m = 0;
        query (1, 1, n);
        if (temp.g == temp.m)
            printf ("%d\n", r - l + 1 - temp.num_m);
        else printf ("%d\n", r - l + 1);
    }
    return 0;
}