#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)         for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)        cout << #x << " = " << x << endl;
#define SIZE(p)         (int)(p).size()
#define MP(a, b)        make_pair((a), (b))
#define ALL(p)          (p).begin(), (p).end()
#define rep(i, n)       for(int (i)=0; (i)<(int)(n); ++(i))
#define REP(i, a, n)    for(int (i)=(a); (i)<(int)(n); ++(i))
#define FOR(i, a, b)    for(int (i)=(int)(a); (i)<=(int)(b); ++(i))
#define FORD(i, b, a)   for(int (i)=(int)(b); (i)>=(int)(a); --(i))
#define CLR(x, y)       memset((x), (y), sizeof((x)))
typedef long long LL;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
int A[200005], n;
bool dp[200005][6];
int Fpos[200005];
pii dpc[200005][6];
inline bool okok(int len,int a,int b) {
    int c=b-a-1;
    return len>=2*c && len<=5*c;
}
void go(int L,int R,int a,int b) {
    if(a>b) return;
    if(L>R) return;
    int len=b-a+1, poslen=R-L+1;
    // printf("L=%d R=%d a=%d b=%d\n",L,R,a,b);
    if(!(poslen>=2*len && poslen<=5*len)) {
        printf("L=%d R=%d a=%d b=%d\n",L,R,a,b);
        exit(0);
    }
    int c=poslen/len;
    // DEBUG(c);
    int k=L;
    map<int,int> cc;
    FOR(i,a,b)
    rep(j,c)
        Fpos[k++]=i,++cc[i];
    // DEBUG(k);
    while(k<=R)  Fpos[k++]=b,++cc[b];
    for(auto x:cc) if(x.second>5)   assert(0);
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("E.in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    scanf("%d",&n);
    static int Lpos[100005], Rpos[100005], C[100005];
    static int Num[200005], Cnt[200005];
    int k=0;
    FOR(i,1,n) {
        scanf("%d",A+i);
        if(!A[i])   continue;
        ++C[A[i]];
        if(Lpos[A[i]]) {
            for(int j=i-1;j;--j) {
                if(A[j]==A[i])  break;
                if(!A[j])       A[j]=A[i],++C[A[i]];
                if(A[j]>A[i])   return puts("-1"),0;
            }
        }
        if(!Lpos[A[i]])  Lpos[A[i]]=i, Num[++k]=A[i];
        Rpos[A[i]]=i;
    }
    if(*max_element(A+1,A+1+n)==0) {
        printf("%d\n",n/2);
        FOR(i,1,n/2*2)  printf("%d ",(i+1)/2);
        rep(i,n-n/2*2)  printf("%d ",(n/2*2+1)/2);
        return 0;
    }
    // FOR(i,1,n)  printf("%d ",A[i]); puts("");
    int N=k;
    dp[0][0]=1; Rpos[0]=0;
    FOR(i,1,N)
    FOR(j,0,5) if(C[Num[i]]+j<=5) {
        int gapLen=Lpos[Num[i]]-Rpos[Num[i-1]]-1;
        if(gapLen<0)    continue;
        FOR(l,0,5) if(C[Num[i]]+j+l<=5 && C[Num[i]]+j+l>=2) {
            FOR(k,0,5) if(k+l<=gapLen && dp[i-1][k] && okok(gapLen-l-k, Num[i-1],Num[i])) {
                dp[i][j]=1, dpc[i][j]=MP(k,l);
            }
        }
    }
    int res=-1, resp;
    FOR(j,0,5) if(j<=n-Rpos[Num[N]] && dp[N][j]) {
        int t=Num[N],len=n-Rpos[Num[N]]-j;
        int l=(len+4)/5, r=len/2;
        if(l>r) t+=-inf;
        else    t+=r;
        if(res==-1 || res<t)    res=t, resp=j;
    }
    printf("%d\n",res);
    if(res==-1)     return 0;
    go(Rpos[Num[N]]+resp+1,n,Num[N]+1,res);
    int a=N, b=resp;
    while(a) {
        FOR(i,Lpos[Num[a]],Rpos[Num[a]])    Fpos[i]=Num[a];
        rep(i,b)                Fpos[Rpos[Num[a]]+1+i]=Num[a];
        rep(i,dpc[a][b].second) Fpos[Lpos[Num[a]]-1-i]=Num[a];
        int gl=Rpos[Num[a-1]]+dpc[a][b].first+1, gr=Lpos[Num[a]]-dpc[a][b].second-1;
        // printf("a=%d b=%d %d %d\n",a,b,dpc[a][b].first,dpc[a][b].second);
        go(gl,gr,Num[a-1]+1,Num[a]-1);
        int aa=a, bb=b;
        a=aa-1, b=dpc[aa][bb].first;
    }
    FOR(i,1,n)  printf("%d ",Fpos[i]);  puts("");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)         for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)        cout << #x << " = " << x << endl;
#define SIZE(p)         (int)(p).size()
#define MP(a, b)        make_pair((a), (b))
#define ALL(p)          (p).begin(), (p).end()
#define rep(i, n)       for(int (i)=0; (i)<(int)(n); ++(i))
#define REP(i, a, n)    for(int (i)=(a); (i)<(int)(n); ++(i))
#define FOR(i, a, b)    for(int (i)=(int)(a); (i)<=(int)(b); ++(i))
#define FORD(i, b, a)   for(int (i)=(int)(b); (i)>=(int)(a); --(i))
#define CLR(x, y)       memset((x), (y), sizeof((x)))
typedef long long LL;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
int A[200005], n;
bool dp[200005][6];
int Fpos[200005];
pii dpc[200005][6];
inline bool okok(int len,int a,int b) {
    int c=b-a-1;
    return len>=2*c && len<=5*c;
}
void go(int L,int R,int a,int b) {
    if(a>b) return;
    if(L>R) return;
    int len=b-a+1, poslen=R-L+1;
    // printf("L=%d R=%d a=%d b=%d\n",L,R,a,b);
    if(!(poslen>=2*len && poslen<=5*len)) {
        printf("L=%d R=%d a=%d b=%d\n",L,R,a,b);
        exit(0);
    }
    int c=poslen-2*len;
    c=2+c/len;
    assert(c<=5);
    map<int,int> cc;
    FOR(i,a,b)
    rep(j,c)    Fpos[L++]=i,++cc[i];
    while(L<=R) Fpos[L++]=b,++cc[b];
    for(auto x:cc) if(x.second>5) {
        printf("L=%d R=%d a=%d b=%d\n",L,R,a,b);
        exit(0);
    }
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("E.in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    scanf("%d",&n);
    static int Lpos[100005], Rpos[100005], C[100005];
    static int Num[200005], Cnt[200005];
    int k=0;
    FOR(i,1,n) {
        scanf("%d",A+i);
        if(!A[i])   continue;
        ++C[A[i]];
        if(Lpos[A[i]]) {
            for(int j=i-1;j;--j) {
                if(A[j]==A[i])  break;
                if(!A[j])       A[j]=A[i],++C[A[i]];
                if(A[j]>A[i])   return puts("-1"),0;
            }
        }
        if(!Lpos[A[i]])  Lpos[A[i]]=i, Num[++k]=A[i];
        Rpos[A[i]]=i;
    }
    if(*max_element(A+1,A+1+n)==0) {
        printf("%d\n",n/2);
        FOR(i,1,n/2*2)  printf("%d ",(i+1)/2);
        rep(i,n-n/2*2)  printf("%d ",(n/2*2+1)/2);
        return 0;
    }
    // FOR(i,1,n)  printf("%d ",A[i]); puts("");
    int N=k;
    dp[0][0]=1; Rpos[0]=0;
    FOR(i,1,N)
    FOR(j,0,5) if(C[Num[i]]+j<=5) {
        int gapLen=Lpos[Num[i]]-Rpos[Num[i-1]]-1;
        if(gapLen<0)    continue;
        FOR(l,0,5) if(C[Num[i]]+j+l<=5 && C[Num[i]]+j+l>=2) {
            FOR(k,0,5) if(k+l<=gapLen && dp[i-1][k] && okok(gapLen-l-k, Num[i-1],Num[i])) {
                dp[i][j]=1, dpc[i][j]=MP(k,l);
            }
        }
    }
    int res=-1, resp;
    FOR(j,0,5) if(j<=n-Rpos[Num[N]] && dp[N][j]) {
        int t=Num[N],len=n-Rpos[Num[N]]-j;
        int l=(len+4)/5, r=len/2;
        if(l>r) t+=-inf;
        else    t+=r;
        if(res==-1 || res<t)    res=t, resp=j;
    }
    printf("%d\n",res);
    if(res==-1)     return 0;
    go(Rpos[Num[N]]+resp+1,n,Num[N]+1,res);
    int a=N, b=resp;
    while(a) {
        FOR(i,Lpos[Num[a]],Rpos[Num[a]])    Fpos[i]=Num[a];
        rep(i,b)                Fpos[Rpos[Num[a]]+1+i]=Num[a];
        rep(i,dpc[a][b].second) Fpos[Lpos[Num[a]]-1-i]=Num[a];
        int gl=Rpos[Num[a-1]]+dpc[a][b].first+1, gr=Lpos[Num[a]]-dpc[a][b].second-1;
        // printf("a=%d b=%d %d %d\n",a,b,dpc[a][b].first,dpc[a][b].second);
        go(gl,gr,Num[a-1]+1,Num[a]-1);
        int aa=a, bb=b;
        a=aa-1, b=dpc[aa][bb].first;
    }
    FOR(i,1,n)  printf("%d ",Fpos[i]);  puts("");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)         for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)        cout << #x << " = " << x << endl;
#define SIZE(p)         (int)(p).size()
#define MP(a, b)        make_pair((a), (b))
#define ALL(p)          (p).begin(), (p).end()
#define rep(i, n)       for(int (i)=0; (i)<(int)(n); ++(i))
#define REP(i, a, n)    for(int (i)=(a); (i)<(int)(n); ++(i))
#define FOR(i, a, b)    for(int (i)=(int)(a); (i)<=(int)(b); ++(i))
#define FORD(i, b, a)   for(int (i)=(int)(b); (i)>=(int)(a); --(i))
#define CLR(x, y)       memset((x), (y), sizeof((x)))
typedef long long LL;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
int A[200005], n;
bool dp[200005][6];
int Fpos[200005];
pii dpc[200005][6];
inline bool okok(int len,int a,int b) {
    int c=b-a-1;
    return len>=2*c && len<=5*c;
}
void go(int L,int R,int a,int b) {
    if(a>b) return;
    if(L>R) return;
    int len=b-a+1, poslen=R-L+1;
    // printf("L=%d R=%d a=%d b=%d\n",L,R,a,b);
    if(!(poslen>=2*len && poslen<=5*len)) {
        printf("L=%d R=%d a=%d b=%d\n",L,R,a,b);
        exit(0);
    }
    int c=poslen-2*len;
    c=2+c/len;
    assert(c<=5);
    map<int,int> cc;
    int LL=L;
    FOR(i,a,b)
    rep(j,c)    Fpos[L++]=i,++cc[i];
    while(L<=R) Fpos[L++]=b,++cc[b];
    for(auto x:cc) if(x.second>5) {
        printf("L=%d R=%d a=%d b=%d\n",LL,R,a,b);
        exit(0);
    }
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("E.in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    scanf("%d",&n);
    static int Lpos[100005], Rpos[100005], C[100005];
    static int Num[200005], Cnt[200005];
    int k=0;
    FOR(i,1,n) {
        scanf("%d",A+i);
        if(!A[i])   continue;
        ++C[A[i]];
        if(Lpos[A[i]]) {
            for(int j=i-1;j;--j) {
                if(A[j]==A[i])  break;
                if(!A[j])       A[j]=A[i],++C[A[i]];
                if(A[j]>A[i])   return puts("-1"),0;
            }
        }
        if(!Lpos[A[i]])  Lpos[A[i]]=i, Num[++k]=A[i];
        Rpos[A[i]]=i;
    }
    if(*max_element(A+1,A+1+n)==0) {
        printf("%d\n",n/2);
        FOR(i,1,n/2*2)  printf("%d ",(i+1)/2);
        rep(i,n-n/2*2)  printf("%d ",(n/2*2+1)/2);
        return 0;
    }
    // FOR(i,1,n)  printf("%d ",A[i]); puts("");
    int N=k;
    dp[0][0]=1; Rpos[0]=0;
    FOR(i,1,N)
    FOR(j,0,5) if(C[Num[i]]+j<=5) {
        int gapLen=Lpos[Num[i]]-Rpos[Num[i-1]]-1;
        if(gapLen<0)    continue;
        FOR(l,0,5) if(C[Num[i]]+j+l<=5 && C[Num[i]]+j+l>=2) {
            FOR(k,0,5) if(k+l<=gapLen && dp[i-1][k] && okok(gapLen-l-k, Num[i-1],Num[i])) {
                dp[i][j]=1, dpc[i][j]=MP(k,l);
            }
        }
    }
    int res=-1, resp;
    FOR(j,0,5) if(j<=n-Rpos[Num[N]] && dp[N][j]) {
        int t=Num[N],len=n-Rpos[Num[N]]-j;
        int l=(len+4)/5, r=len/2;
        if(l>r) t+=-inf;
        else    t+=r;
        if(res==-1 || res<t)    res=t, resp=j;
    }
    printf("%d\n",res);
    if(res==-1)     return 0;
    go(Rpos[Num[N]]+resp+1,n,Num[N]+1,res);
    int a=N, b=resp;
    while(a) {
        FOR(i,Lpos[Num[a]],Rpos[Num[a]])    Fpos[i]=Num[a];
        rep(i,b)                Fpos[Rpos[Num[a]]+1+i]=Num[a];
        rep(i,dpc[a][b].second) Fpos[Lpos[Num[a]]-1-i]=Num[a];
        int gl=Rpos[Num[a-1]]+dpc[a][b].first+1, gr=Lpos[Num[a]]-dpc[a][b].second-1;
        // printf("a=%d b=%d %d %d\n",a,b,dpc[a][b].first,dpc[a][b].second);
        go(gl,gr,Num[a-1]+1,Num[a]-1);
        int aa=a, bb=b;
        a=aa-1, b=dpc[aa][bb].first;
    }
    FOR(i,1,n)  printf("%d ",Fpos[i]);  puts("");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)         for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)        cout << #x << " = " << x << endl;
#define SIZE(p)         (int)(p).size()
#define MP(a, b)        make_pair((a), (b))
#define ALL(p)          (p).begin(), (p).end()
#define rep(i, n)       for(int (i)=0; (i)<(int)(n); ++(i))
#define REP(i, a, n)    for(int (i)=(a); (i)<(int)(n); ++(i))
#define FOR(i, a, b)    for(int (i)=(int)(a); (i)<=(int)(b); ++(i))
#define FORD(i, b, a)   for(int (i)=(int)(b); (i)>=(int)(a); --(i))
#define CLR(x, y)       memset((x), (y), sizeof((x)))
typedef long long LL;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
int A[200005], n;
bool dp[200005][6];
int Fpos[200005];
pii dpc[200005][6];
inline bool okok(int len,int a,int b) {
    int c=b-a-1;
    return len>=2*c && len<=5*c;
}
void go(int L,int R,int a,int b) {
    if(a>b) return;
    if(L>R) return;
    int len=b-a+1, poslen=R-L+1;
    // printf("L=%d R=%d a=%d b=%d\n",L,R,a,b);
    if(!(poslen>=2*len && poslen<=5*len)) {
        printf("L=%d R=%d a=%d b=%d\n",L,R,a,b);
        exit(0);
    }
    static int cc[200005];
    FOR(i,a,b)  cc[i]=0;
    int c=poslen;
    FOR(k,1,5) {
        for(int i=a;i<=b && c;++i)
            ++cc[i], --c;
    }
    FOR(i,1,b)
    rep(j,cc[i])    Fpos[L++]=i;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("E.in", "r", stdin);
    // freopen("out", "w", stdout);
#endif
    scanf("%d",&n);
    static int Lpos[100005], Rpos[100005], C[100005];
    static int Num[200005], Cnt[200005];
    int k=0;
    FOR(i,1,n) {
        scanf("%d",A+i);
        if(!A[i])   continue;
        ++C[A[i]];
        if(Lpos[A[i]]) {
            for(int j=i-1;j;--j) {
                if(A[j]==A[i])  break;
                if(!A[j])       A[j]=A[i],++C[A[i]];
                if(A[j]>A[i])   return puts("-1"),0;
            }
        }
        if(!Lpos[A[i]])  Lpos[A[i]]=i, Num[++k]=A[i];
        Rpos[A[i]]=i;
    }
    if(*max_element(A+1,A+1+n)==0) {
        printf("%d\n",n/2);
        FOR(i,1,n/2*2)  printf("%d ",(i+1)/2);
        rep(i,n-n/2*2)  printf("%d ",(n/2*2+1)/2);
        return 0;
    }
    // FOR(i,1,n)  printf("%d ",A[i]); puts("");
    int N=k;
    dp[0][0]=1; Rpos[0]=0;
    FOR(i,1,N)
    FOR(j,0,5) if(C[Num[i]]+j<=5) {
        int gapLen=Lpos[Num[i]]-Rpos[Num[i-1]]-1;
        if(gapLen<0)    continue;
        FOR(l,0,5) if(C[Num[i]]+j+l<=5 && C[Num[i]]+j+l>=2) {
            FOR(k,0,5) if(k+l<=gapLen && dp[i-1][k] && okok(gapLen-l-k, Num[i-1],Num[i])) {
                dp[i][j]=1, dpc[i][j]=MP(k,l);
            }
        }
    }
    int res=-1, resp;
    FOR(j,0,5) if(j<=n-Rpos[Num[N]] && dp[N][j]) {
        int t=Num[N],len=n-Rpos[Num[N]]-j;
        int l=(len+4)/5, r=len/2;
        if(l>r) t+=-inf;
        else    t+=r;
        if(res==-1 || res<t)    res=t, resp=j;
    }
    printf("%d\n",res);
    if(res==-1)     return 0;
    go(Rpos[Num[N]]+resp+1,n,Num[N]+1,res);
    int a=N, b=resp;
    while(a) {
        FOR(i,Lpos[Num[a]],Rpos[Num[a]])    Fpos[i]=Num[a];
        rep(i,b)                Fpos[Rpos[Num[a]]+1+i]=Num[a];
        rep(i,dpc[a][b].second) Fpos[Lpos[Num[a]]-1-i]=Num[a];
        int gl=Rpos[Num[a-1]]+dpc[a][b].first+1, gr=Lpos[Num[a]]-dpc[a][b].second-1;
        // printf("a=%d b=%d %d %d\n",a,b,dpc[a][b].first,dpc[a][b].second);
        go(gl,gr,Num[a-1]+1,Num[a]-1);
        int aa=a, bb=b;
        a=aa-1, b=dpc[aa][bb].first;
    }
    FOR(i,1,n)  printf("%d ",Fpos[i]);  puts("");
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    vector<int> noLarger(n);
    {
    	int curMin = 1000000;
    	FORD (i, n - 1, 0) {
    		if (a[i] != 0) {
    			if (a[i] > curMin) return NULL;
    			curMin = a[i];
    		}
    		noLarger[i] = curMin;
    	}
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2 && mx[i] / 10 < noLarger[i]) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;
    	if (mx[i] % 10 == 7) mx[i]--;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }
    if (mn.back() / 10 == mx.back() / 10 && mx.back() % 10 == 0) {
    	return NULL;
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
	    if (sol[lastDigit] % 10 == 0) {
	    	if (lastDigit == n - 1) return NULL;
	    	sol[lastDigit + 1] = sol[lastDigit] + 1;
	    }
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
        break;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (rem < n && sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else {
    			// cout << mx[1] << ' ';
    			return NULL;
    		}
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	cout << sol->back() << '\n';
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    vector<int> noLarger(n);
    {
    	int curMin = 100000;
    	FORD (i, n - 1, 0) {
    		if (a[i] != 0) {
    			if (a[i] > curMin) return NULL;
    			curMin = a[i];
    		}
    		noLarger[i] = curMin;
    	}
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2 && mx[i] / 10 < noLarger[i]) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;
    	if (mx[i] % 10 == 7) mx[i]--;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
	    if (sol[lastDigit] % 10 == 0 && lastDigit + 1 < n) {
	    	sol[lastDigit + 1] = sol[lastDigit] + 1;
	    }
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
    	break;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (rem < n && sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else {
    			return NULL;
    		}
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	cout << sol->back() << '\n';
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    vector<int> noLarger(n);
    {
    	int curMin = 1000000;
    	FORD (i, n - 1, 0) {
    		if (a[i] != 0) {
    			if (a[i] > curMin) return NULL;
    			curMin = a[i];
    		}
    		noLarger[i] = curMin;
    	}
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2 && mx[i] / 10 < noLarger[i]) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;
    	if (mx[i] % 10 == 7) mx[i]--;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }
    if (mn.back() / 10 == mx.back() / 10 && mx.back() % 10 == 0) {
    	return NULL;
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
	    if (sol[lastDigit] % 10 == 0) {
	    	if (lastDigit == n - 1) return NULL;
	    	sol[lastDigit + 1] = sol[lastDigit] + 1;
	    }
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (rem < n && sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else {
    			// cout << mx[1] << ' ';
    			return NULL;
    		}
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	cout << sol->back() << '\n';
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    vector<int> noLarger(n);
    {
    	int curMin = 100000;
    	FORD (i, n - 1, 0) {
    		if (a[i] != 0) {
    			if (a[i] > curMin) return NULL;
    			curMin = a[i];
    		}
    		noLarger[i] = curMin;
    	}
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2 && mx[i] / 10 < noLarger[i]) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;
    	if (mx[i] % 10 == 7) mx[i]--;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	if (mx[lastDigit] % 10 == 4 && mn[lastDigit] % 10 < 4)
    	sol[lastDigit] = mx[lastDigit] - 1;
    	else sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
	    if (sol[lastDigit] % 10 == 0 && lastDigit + 1 < n) {
	    	sol[lastDigit + 1] = sol[lastDigit] + 1;
	    }
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
    	break;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else {
    			return NULL;
    		}
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	cout << sol->back() << '\n';
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }
    if (mn.back() / 10 == mx.back() / 10 && mx.back() % 10 == 0) {
    	return NULL;
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
	    if (sol[lastDigit] % 10 == 0) {
	    	if (lastDigit == n - 1) return NULL;
	    	sol[lastDigit + 1] = sol[lastDigit] + 1;
	    }
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (rem < n && sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else return NULL;
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    vector<int> noLarger(n);
    {
    	int curMin = 100000;
    	FORD (i, n - 1, 0) {
    		if (a[i] != 0) {
    			if (a[i] > curMin) return NULL;
    			curMin = a[i];
    		}
    		noLarger[i] = curMin;
    	}
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2 && mx[i] / 10 < noLarger[i]) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;
    	if (mx[i] % 10 == 7) mx[i]--;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
	    if (sol[lastDigit] % 10 == 0 && lastDigit + 1 < n) {
	    	sol[lastDigit + 1] = sol[lastDigit] + 1;
	    }
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
    	break;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else {
    			return NULL;
    		}
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	cout << sol->back() << '\n';
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    vector<int> noLarger(n);
    {
    	int curMin = 10000000;
    	FORD (i, n - 1, 0) {
    		if (a[i] != 0) {
    			if (a[i] > curMin) return NULL;
    			curMin = a[i];
    		}
    		noLarger[i] = curMin;
    	}
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2 && mx[i] / 10 < noLarger[i]) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;
    	if (mx[i] % 10 == 7) mx[i]--;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }
    if (mn.back() / 10 == mx.back() / 10 && mx.back() % 10 == 0) {
    	return NULL;
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
	    if (sol[lastDigit] % 10 == 0) {
	    	if (lastDigit == n - 1) return NULL;
	    	sol[lastDigit + 1] = sol[lastDigit] + 1;
	    }
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
        break;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (rem < n && sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else {
    			// cout << mx[1] << ' ';
    			return NULL;
    		}
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	cout << sol->back() << '\n';
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    vector<int> noLarger(n);
    {
    	int curMin = 100000;
    	FORD (i, n - 1, 0) {
    		if (a[i] != 0) {
    			if (a[i] > curMin) return NULL;
    			curMin = a[i];
    		}
    		noLarger[i] = curMin;
    	}
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2 && mx[i] / 10 < noLarger[i]) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;
    	if (mx[i] % 10 == 7) mx[i]--;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
    	break;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (rem < n && sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else {
    			return NULL;
    		}
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	cout << sol->back() << '\n';
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    vector<int> noLarger(n);
    {
    	int curMin = 100000;
    	FORD (i, n - 1, 0) {
    		if (a[i] != 0) {
    			if (a[i] > curMin) return NULL;
    			curMin = a[i];
    		}
    		noLarger[i] = curMin;
    	}
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2 && mx[i] / 10 < noLarger[i]) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;
    	if (mx[i] % 10 == 7) mx[i]--;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	if (mx[lastDigit] % 10 == 4 && mn[lastDigit] % 10 < 4)
    	sol[lastDigit] = mx[lastDigit] - 1;
    	else sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
	    if (sol[lastDigit] % 10 == 0 && lastDigit + 1 < n) {
	    	sol[lastDigit + 1] = sol[lastDigit] + 1;
	    }
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
    	break;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else {
    			return NULL;
    		}
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    if (sol.back() != sol[sol.size() - 2]) return NULL;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	cout << sol->back() << '\n';
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    vector<int> noLarger(n);
    {
    	int curMin = 100000;
    	FORD (i, n - 1, 0) {
    		if (a[i] != 0) {
    			if (a[i] > curMin) return NULL;
    			curMin = a[i];
    		}
    		noLarger[i] = curMin;
    	}
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2 && mx[i] / 10 < noLarger[i]) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;
    	if (mx[i] % 10 == 7) mx[i]--;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
	    if (sol[lastDigit] % 10 == 0) {
	    	if (lastDigit == n - 1) return NULL;
	    	sol[lastDigit + 1] = sol[lastDigit] + 1;
	    }
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
    	break;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (rem < n && sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else {
    			return NULL;
    		}
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	cout << sol->back() << '\n';
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for(int i = a; i < b + 1; ++i)
#define FORD(i, a, b) for(int i = a; i >= b; --i)

#define next(type, i) type i; cin >> i;
#define nextVector(type, v, size) vector<type> v(size); { for (int i = 0 ; i < size ; i++) cin >> v[i]; }

typedef long long LL;

using namespace std;

vector<int> *solve() {
	next(int, n);
    nextVector(int, a, n);
    if (n == 1) return NULL;

    vector<int> mn(n);
    vector<int> mx(n);
    mn[0] = mx[0] = 10;
    if (a[0] > 1) {
    	return NULL;
    }

    FOR (i, 1, n - 1) {
    	mn[i] = mn[i - 1] + 1;
    	mx[i] = mx[i - 1] + 1;
    	if (mx[i] % 10 >= 2) mx[i] += 10 - mx[i] % 10;
    	if (mn[i] % 10 >= 5) mn[i] += 10 - mn[i] % 10;

    	if (a[i] != 0) {
    		mx[i] = min(mx[i], a[i] * 10 + 4);
    		mn[i] = max(mn[i], a[i] * 10 + 0);
    		if (mx[i] < mn[i]) {
    			return NULL;
    		}
    	}
    }
    if (mn.back() / 10 == mx.back() / 10 && mx.back() % 10 == 0) {
    	return NULL;
    }

    vector<int> sol(n);
    int lastDigit = -1;
    FORD (i, n - 1, 0) if (a[i] != 0) {
    	lastDigit = i;
    	break;
    }
    if (lastDigit != -1) {
    	sol[lastDigit] = mx[lastDigit];
    	FORD(i, lastDigit - 1, 0) {
    		int nextDig = sol[i + 1];
    		int curDig = nextDig % 10 == 0 ?  nextDig - 6 : nextDig - 1;
    		sol[i] = min(mx[i], curDig);
    	}
	    if (sol[lastDigit] % 10 == 0) {
	    	if (lastDigit == n - 1) return NULL;
	    	sol[lastDigit + 1] = sol[lastDigit] + 1;
	    }
    }

    int rem = 0;
    int fillWith = 1;

    FORD(i, n - 1, 0) if (sol[i] == 0) rem++;
    else {
    	fillWith = sol[i] / 10 + 1;
    }

    while (rem > 0) {
    	if (rem == 1) {
    		if (rem < n && sol[n - rem - 1] % 10 < 4) {
    			sol[n - rem] = sol[n - rem - 1] + 1;
    			rem--;
    		} else return NULL;
    	} else {
	    	int x = 2;
	    	int startWith = n - rem;
	    	FOR (i, 0, x - 1) sol[startWith + i] = fillWith*10 + i;
	    	rem -= x;
	    	fillWith++;
    	}
    }

    FOR (i, 0, n - 1) sol[i] /= 10;
    return new vector<int>(sol);
}

int main() {
    ios_base::sync_with_stdio(false);

    vector<int> *sol = solve();
    if (sol == NULL) {
    	cout << -1;
    } else {
    	cout << sol->back() << '\n';
    	FOR (i, 0, sol->size() - 1) cout << sol->at(i) << ' ';
    }
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <forward_list>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <regex>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

#define FORI(i,n) for(int i=0; i<(int)(n); ++i)
#define FORIR(i,n) for(int i=(int)((n)-1); i>=0; --i)
#define MP(a,b) make_pair(a,b)
#define MT(a...) make_tuple(a)
#define ALL(L) (L).begin(),(L).end()
#define ALLR(L) (L).rbegin(),(L).rend()
#define SZ(L) (L).size()
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define SORTED_PAIR(a,b) MIN(a,b),MAX(a,b)
#define INF (1<<29)
#define EPS (1e-9)

typedef unsigned int uint;
typedef unsigned long long ull;
typedef signed long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main() {
  int n,t;
  vector<int> v;
  vector<tuple<int,int>> m;
  cin >> n;
  FORI(i,n){
    cin >> t;
    v.push_back(t);
  }
  if(v[0] > 1) {
    cout << -1 << endl;
    return 0;
  }
  m.push_back(MT(5,5));
  for(int i = 1; i < n; ++i){
    int mi, mx;
    mi = get<0>(m[i - 1]) + 1;
    mx = (get<1>(m[i - 1]) % 5 == 0 ? get<1>(m[i - 1]) + 1 : get<1>(m[i - 1]) + 5 - (get<1>(m[i - 1]) % 5));
    if(v[i] == 0) {
      m.push_back(MT(mi,mx));
    } else if(v[i] * 5 + 4 < mi || v[i] * 5 > mx) {
      cout << -1 << endl;
      return 0;
    } else {
      mi = max(mi, v[i] * 5);
      mx = min(mx, v[i] * 5 + 4);
      m.push_back(MT(mi, mx));
    }
  }
  list<int> res;
  if(get<1>(m[n - 1]) % 5 == 0) {
    get<1>(m[n - 1])--;
  }
  res.push_front(get<1>(m[n - 1]) / 5);
  for(int i = n - 2; i >= 0; --i) {
    int mx = min(get<1>(m[i + 1]) - 1, get<1>(m[i]));
    get<1>(m[i]) = mx;
    res.push_front(mx / 5);
  }
  cout << *(--res.end()) << endl;
  for(auto i : res) {
    cout << i << " ";
  }
  cout << endl;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <forward_list>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <regex>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

#define FORI(i,n) for(int i=0; i<(int)(n); ++i)
#define FORIR(i,n) for(int i=(int)((n)-1); i>=0; --i)
#define MP(a,b) make_pair(a,b)
#define MT(a...) make_tuple(a)
#define ALL(L) (L).begin(),(L).end()
#define ALLR(L) (L).rbegin(),(L).rend()
#define SZ(L) (L).size()
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define SORTED_PAIR(a,b) MIN(a,b),MAX(a,b)
#define INF (1<<29)
#define EPS (1e-9)

typedef unsigned int uint;
typedef unsigned long long ull;
typedef signed long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main() {
  int n,t;
  vector<int> v;
  vector<tuple<int,int>> m;
  cin >> n;
  FORI(i,n){
    cin >> t;
    v.push_back(t);
  }
  if(v[0] > 1) {
    cout << -1 << endl;
    return 0;
  }
  m.push_back(MT(5,5));
  for(int i = 1; i < n; ++i){
    int mi, mx;
    mi = get<0>(m[i - 1]) + 1;
    mx = (get<1>(m[i - 1]) % 5 == 0 ? get<1>(m[i - 1]) + 1 : get<1>(m[i - 1]) + 5 - (get<1>(m[i - 1]) % 5));
    if(v[i] == 0) {
      m.push_back(MT(mi,mx));
    } else if(v[i] * 5 + 4 < mi || v[i] * 5 > mx) {
      cout << -1 << endl;
      return 0;
    } else {
      mi = max(mi, v[i] * 5);
      mx = min(mx, v[i] * 5 + 4);
      m.push_back(MT(mi, mx));
    }
  }
  list<int> res;
  if(get<1>(m[n - 1]) % 5 == 0) {
    get<1>(m[n - 1])--;
  }
  res.push_front(get<1>(m[n - 1]) / 5);
  if(*(res.begin()) != v[n - 1]) {
    cout << -1 << endl;
    return 0;
  }
  for(int i = n - 2; i >= 0; --i) {
    int mx = min(get<1>(m[i + 1]) - 1, get<1>(m[i]));
    get<1>(m[i]) = mx;
    res.push_front(mx / 5);
  }
  cout << *(--res.end()) << endl;
  for(auto i : res) {
    cout << i << " ";
  }
  cout << endl;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <forward_list>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <regex>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

#define FORI(i,n) for(int i=0; i<(int)(n); ++i)
#define FORIR(i,n) for(int i=(int)((n)-1); i>=0; --i)
#define MP(a,b) make_pair(a,b)
#define MT(a...) make_tuple(a)
#define ALL(L) (L).begin(),(L).end()
#define ALLR(L) (L).rbegin(),(L).rend()
#define SZ(L) (L).size()
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define SORTED_PAIR(a,b) MIN(a,b),MAX(a,b)
#define INF (1<<29)
#define EPS (1e-9)

typedef unsigned int uint;
typedef unsigned long long ull;
typedef signed long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main() {
  int n,t;
  vector<int> v;
  vector<tuple<int,int>> m;
  cin >> n;
  FORI(i,n){
    cin >> t;
    v.push_back(t);
  }
  if(v[0] > 1) {
    cout << -1 << endl;
    return 0;
  }
  m.push_back(MT(5,5));
  for(int i = 1; i < n; ++i){
    int mi, mx;
    mi = get<0>(m[i - 1]) + 1;
    mx = (get<1>(m[i - 1]) % 5 == 0 ? get<1>(m[i - 1]) + 1 : get<1>(m[i - 1]) + 5 - (get<1>(m[i - 1]) % 5));
    if(v[i] == 0) {
      m.push_back(MT(mi,mx));
    } else if(v[i] * 5 + 4 < mi || v[i] * 5 > mx) {
      cout << -1 << endl;
      return 0;
    } else {
      mi = max(mi, v[i] * 5);
      mx = min(mx, v[i] * 5 + 4);
      m.push_back(MT(mi, mx));
    }
  }
  list<int> res;
  cout << (get<1>(m[n - 1]) - 1) / 5 << endl;
  res.push_front(get<1>(m[n - 1]) / 5);
  for(int i = n - 2; i >= 0; --i) {
    int mx = min(get<1>(m[i + 1]) - 1, get<1>(m[i]));
    get<1>(m[i]) = mx;
    res.push_front(mx / 5);
  }
  for(auto i : res) {
    cout << i << " ";
  }
  cout << endl;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <forward_list>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <regex>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

#define FORI(i,n) for(int i=0; i<(int)(n); ++i)
#define FORIR(i,n) for(int i=(int)((n)-1); i>=0; --i)
#define MP(a,b) make_pair(a,b)
#define MT(a...) make_tuple(a)
#define ALL(L) (L).begin(),(L).end()
#define ALLR(L) (L).rbegin(),(L).rend()
#define SZ(L) (L).size()
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define SORTED_PAIR(a,b) MIN(a,b),MAX(a,b)
#define INF (1<<29)
#define EPS (1e-9)

typedef unsigned int uint;
typedef unsigned long long ull;
typedef signed long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main() {
  int n,t;
  vector<int> v;
  vector<tuple<int,int>> m;
  cin >> n;
  FORI(i,n){
    cin >> t;
    v.push_back(t);
  }
  if(v[0] > 1) {
    cout << -1 << endl;
    return 0;
  }
  m.push_back(MT(5,5));
  for(int i = 1; i < n; ++i){
    int mi, mx;
    mi = get<0>(m[i - 1]) + 1;
    mx = (get<1>(m[i - 1]) % 5 == 0 ? get<1>(m[i - 1]) + 1 : get<1>(m[i - 1]) + 5 - (get<1>(m[i - 1]) % 5));
    if(v[i] == 0) {
      m.push_back(MT(mi,mx));
    } else if(v[i] * 5 + 4 < mi || v[i] * 5 > mx) {
      cout << -1 << endl;
      return 0;
    } else {
      mi = max(mi, v[i] * 5);
      mx = min(mx, v[i] * 5 + 4);
      m.push_back(MT(mi, mx));
    }
  }
  list<int> res;
  if(get<1>(m[n - 1]) % 5 == 0) {
    get<1>(m[n - 1])--;
  }
  res.push_front(get<1>(m[n - 1]) / 5);
  if(v[n - 1] != 0 && *(res.begin()) != v[n - 1]) {
    cout << -1 << endl;
    return 0;
  }
  for(int i = n - 2; i >= 0; --i) {
    int mx = min(get<1>(m[i + 1]) - 1, get<1>(m[i]));
    get<1>(m[i]) = mx;
    res.push_front(mx / 5);
  }
  cout << *(--res.end()) << endl;
  for(auto i : res) {
    cout << i << " ";
  }
  cout << endl;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <forward_list>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <regex>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

#define FORI(i,n) for(int i=0; i<(int)(n); ++i)
#define FORIR(i,n) for(int i=(int)((n)-1); i>=0; --i)
#define MP(a,b) make_pair(a,b)
#define MT(a...) make_tuple(a)
#define ALL(L) (L).begin(),(L).end()
#define ALLR(L) (L).rbegin(),(L).rend()
#define SZ(L) (L).size()
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define SORTED_PAIR(a,b) MIN(a,b),MAX(a,b)
#define INF (1<<29)
#define EPS (1e-9)

typedef unsigned int uint;
typedef unsigned long long ull;
typedef signed long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main() {
  int n,t;
  vector<int> v;
  vector<tuple<int,int>> m;
  cin >> n;
  FORI(i,n){
    cin >> t;
    v.push_back(t);
  }
  if(v[0] > 1) {
    cout << -1 << endl;
    return 0;
  }
  m.push_back(MT(5,5));
  for(int i = 1; i < n; ++i){
    int mi, mx;
    mi = get<0>(m[i - 1]) + 1;
    mx = (get<1>(m[i - 1]) % 5 == 0 ? get<1>(m[i - 1]) + 1 : get<1>(m[i - 1]) + 5 - (get<1>(m[i - 1]) % 5));
    if(v[i] == 0) {
      m.push_back(MT(mi,mx));
    } else if(v[i] * 5 + 4 < mi || v[i] * 5 > mx) {
      cout << -1 << endl;
      return 0;
    } else {
      mi = max(mi, v[i] * 5);
      mx = min(mx, v[i] * 5 + 4);
      m.push_back(MT(mi, mx));
    }
  }
  list<int> res;
  t = get<1>(m[n - 1]);
  if(t % 5 == 0) {
    --t;
  }
  res.push_front(t / 5);
  for(int i = n - 2; i >= 0; --i) {
    int mx = min(get<1>(m[i + 1]) - 1, get<1>(m[i]));
    get<1>(m[i]) = mx;
    res.push_front(mx / 5);
  }
  cout << *(--res.end()) << endl;
  for(auto i : res) {
    cout << i << " ";
  }
  cout << endl;
}


#include <stdio.h>

const int maxn = 201000;

int num[maxn+1],first[maxn+1],last[maxn+1];
int a[maxn+1];
bool f[maxn+1][4+1][5+1];
int pa[maxn+1][4+1][5+1];
int pb[maxn+1][4+1][5+1];
int n,top;
bool success;

void init()
{
    int i;
    scanf("%d",&n);
    for (i=1; i<=n; ++i)
        scanf("%d",&a[i]);
    top=0;
    for (i=1; i<=n; ++i)
        if (a[i])
        {
            if ((top==0)||(a[i]!=num[top]))
            {
                num[++top]=a[i];
                first[top]=last[top]=i;
            }
            else last[top]=i;
        }
}

bool ky(int len,int a,int b)
{
    return (len>=2*(b-a+1))&&(len<=5*(b-a+1));
}

void fill(int start,int stop,int na,int nb)
{
    int i,j,p,yao,dy;
    if (start>stop)
        return;
    for (yao=5; yao>=2; --yao)
        if (stop-start+1>=yao*(nb-na+1))
            break;
    dy=(stop-start+1)%(nb-na+1);
    p=start;
    for (i=na; i<=nb; ++i)
    {
        if (dy)
        {
            for (j=1; j<=yao+1; ++j)
                a[p++]=i;
        }
        else {
            for (j=1; j<=yao; ++j)
                a[p++]=i;
        }
        if (dy)
            --dy;
    }
}

void work()
{
    int i,j,k,t,j2,k2;
    for (i=1; i<top; ++i)
        if (num[i]>num[i+1])
        {
            success=false;
            return;
        }
    if (top==0)
    {
        fill(1,n,1,n/2);
        success=true;
        return;
    }
    for (i=1; i<=top; ++i)
        for (j=0; j<=4; ++j)
            for (k=2; k<=5; ++k)
            {
                f[i][j][k]=false;
                if ((first[i]-j>=1)&&(first[i]-j+k-1>=last[i])&&(first[i]-j+k-1<=n))
                {}
                else continue;

                if ((i==1)&&(!ky(first[i]-j-1,1,num[i]-1)))
                    continue;
                if ((i==top)&&(first[i]-j+k-1==n-1))
                    continue;
                if (i==1)
                {
                    f[i][j][k]=true;
                    continue;
                }
                for (j2=0; j2<=4; ++j2)
                {
                    if (f[i][j][k])
                        break;
                    for (k2=2; k2<=5; ++k2)
                        if (f[i-1][j2][k2])
                        {
                            if (first[i]-j>first[i-1]-j2+k2-1)
                                if (ky(first[i]-j-1-(first[i-1]-j2+k2-1),num[i-1]+1,num[i]-1))
                                {
                                    f[i][j][k]=true;
                                    pa[i][j][k]=j2;
                                    pb[i][j][k]=k2;
                                    break;
                                }
                        }
                }
            }
    j=-1;
    for (j2=0; j2<=4; ++j2)
    {
        for (k2=2; k2<=5; ++k2)
            if (f[top][j2][k2])
            {
                success=true;
                if ((j==-1)||(-j2+k2<-j+k))
                {
                    j=j2;
                    k=k2;
                }
            }
    }
    if (!success)
        return;
    for (i=top; i>=1; --i)
    {
        for (t=first[i]-j; t<=first[i]-j+k-1; ++t)
            a[t]=num[i];
        if (i==top) //later portion
            fill(first[i]-j+k,n,num[i]+1,num[i]+(n-(first[i]-j+k)+1)/2);
        if (i==1)
            fill(1,first[i]-j-1,1,num[i]-1);
        else {
            j2=pa[i][j][k];
            k2=pb[i][j][k];
            fill(first[i-1]-j2+k2,first[i]-j-1,num[i-1]+1,num[i]-1);
            j=j2;
            k=k2;
        }
    }
}

void output()
{
    int i;
    if (!success)
        printf("-1\n");
    else {
        printf("%d\n",a[n]);
        for (i=1; i<=n; ++i)
        {
            printf("%d",a[i]);
            if (i==n)
                printf("\n");
            else printf(" ");
        }
    }
}

int main()
{
    //freopen("E.in","r",stdin);
    init();
    work();
    output();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <math.h>
#include <time.h>

using namespace std;

#define ERROR {cout << -1 << endl; return 0;}

int minC = 2;
int maxC = 5;

struct book
{
    int value = -1;
    int begin = -1;
    int end = -1;
    int start = -1;
    int count = 0;
    book *prev = 0;
    book *next = 0;

    bool take_place(book *p, int v)
    {
        prev = p;
        if (prev)
            prev->next = this;
        if (value == -1)
            value = v;
        if (value == 1)
            begin = start = 0;
        if (end - begin >= maxC)
            return false;
        if (value != 1)
        {
            int prev_end = prev->start + prev->count;
            if ((end != -1 && prev_end > end) ||
                (begin != -1 && prev_end > begin))
                return false;
            start = prev_end;
        }
        count = max(minC, end - start + 1);
        if (count > maxC)
        {
            start = end - maxC + 1;
            count = end - start + 1;
            if (!prev->expand(start))
                return false;
        }
        return true;
    }

    bool expand(int p)
    {
        if (p - start > maxC)
        {
            if (!prev || !prev->expand(p - maxC))
                return false;
            start = p - maxC;
        }
        if (next && next->begin != -1 && next->begin < p)
            return false;
        count = p - start;
        return true;
    }
};

int n, t;
map<int, book> d;

#ifdef LOCAL_EXECUTION
#include "local.h"
#endif

int main()
{
    cin >> n;
    d.clear();
    for (int i = 0; i < n; i++)
    {
        cin >> t;
        if (t == 0)
            continue;
        d[t].value = t;
        d[t].count++;
        if (d[t].begin == -1)
            d[t].begin = i;
        d[t].end = i;
    }

    if (!d.empty())
    for (int i = 1; i <= d.rbegin()->first; i++)
        if (!d[i].take_place(i == 1 ? 0 : &d[i-1], i))
            ERROR;
    int end = d.empty() ? 0 : d[d.rbegin()->first].start + d[d.rbegin()->first].count;
    int tail = n - end;
    if (tail > 0)
    {
        if (tail == 1)
        {
            if (!d[d.rbegin()->first].expand(n))
                ERROR;
        }
        else
        {
            int end_val = d.empty() ? 1 : d.rbegin()->first + 1;
            int add = tail / 2;
            for (int i = 0; i < add; i++)
                d[end_val++].count = 2;
            if (tail % 2 == 1)
                d[d.rbegin()->first].count++;
        }
    }
    else if (tail < 0)
        ERROR;

    cout << d.rbegin()->first << endl;
    for (int i = 1; i <= d.rbegin()->first; i++)
    for (int j = 0; j < d[i].count; j++)
        cout << i << " ";

    return 0;
}

#ifdef LOCAL_EXECUTION
PROGRAM_END
}
#endif


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <math.h>
#include <time.h>

using namespace std;

#define ERROR {cout << -1 << endl; return 0;}

int minC = 2;
int maxC = 5;

struct book
{
    int value = -1;
    int begin = -1;
    int end = -1;
    int start = -1;
    int count = 0;
    book *prev = 0;
    book *next = 0;

    bool take_place(book *p, int v)
    {
        prev = p;
        if (prev)
            prev->next = this;
        if (value == -1)
            value = v;
        if (value == 1)
            begin = start = 0;
        if (end - begin >= maxC)
            return false;
        if (value != 1)
        {
            int prev_end = prev->start + prev->count;
            if (end != -1 && prev_end > end)
                return false;
            if (begin != -1 && prev_end > begin)
                return false;
            start = prev_end;
        }
        count = max(minC, end - start + 1);
        if (count > maxC)
        {
            start = end - maxC + 1;
            count = end - start + 1;
            if (!prev->expand(start))
                return false;
        }
        return true;
    }

    bool expand(int p)
    {
        if (p - start > maxC)
        {
            if (!prev || !prev->expand(p - maxC))
                return false;
            start = p - maxC;
        }
        if (next && next->begin != -1 && next->begin < p - start)
            return false;
        count = p - start;
        return true;
    }
};

int n, t;
map<int, book> d;

#ifdef LOCAL_EXECUTION
#include "local.h"
#endif

int main()
{
    cin >> n;
    d.clear();
    for (int i = 0; i < n; i++)
    {
        cin >> t;
        if (t == 0)
            continue;
        d[t].value = t;
        d[t].count++;
        if (d[t].begin == -1)
            d[t].begin = i;
        d[t].end = i;
    }

    if (!d.empty())
    for (int i = 1; i <= d.rbegin()->first; i++)
        if (!d[i].take_place(i == 1 ? 0 : &d[i-1], i))
            ERROR;
    int end = d.empty() ? 0 : d[d.rbegin()->first].start + d[d.rbegin()->first].count;
    int tail = n - end;
    if (tail > 0)
    {
        if (tail == 1)
        {
            if (!d[d.rbegin()->first].expand(n))
                ERROR;
        }
        else
        {
            int end_val = d.empty() ? 1 : d.rbegin()->first + 1;
            int add = tail / 2;
            for (int i = 0; i < add; i++)
                d[end_val++].count = 2;
            if (tail % 2 == 1)
                d[d.rbegin()->first].count++;
        }
    }
    else if (tail < 0)
        ERROR;

    cout << d.rbegin()->first << endl;
    for (int i = 1; i <= d.rbegin()->first; i++)
    for (int j = 0; j < d[i].count; j++)
        cout << i << " ";

    return 0;
}

#ifdef LOCAL_EXECUTION
PROGRAM_END
}
#endif


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
    tree<int, null_type , less<int>, rb_tree_tag,tree_order_statistics_node_update> tr;
    // tr.order_of_key
    // tr.insert(a)
   struct alo{
   int val , id;
   };
   bool comp(alo a, alo b ){
   return a.val > b.val || (a.val == b.val && a.id > b.id);
   }
   alo a[1234];
    main()
{
    int n , k;
    cin >> n >> k;
    for(int i = 1 ; i <= n ;i ++)
        cin >> a[i].val , a[i].id = i;
        if(n == 1){
            return cout << 0 <<" "<<0,0;
        }

    sort(a + 1 , a + n+ 1,comp);
    vector<pair<int,int> > ans;
    int prev = a[1].val - a[n].val;
    if(prev)
    {
    ans.push_back({a[1].id,a[n].id});
    a[1].val --;
    a[n].val ++;
    sort(a + 1 , a + n+ 1,comp);
    }
    for(int i = 2 ; i <= k and prev;i ++)
    {

                prev = a[1].val - a[n].val;
                a[1].val --;
                a[n].val ++;
                ans.push_back({a[1].id,a[n].id});
        sort(a + 1 , a + n + 1, comp);
    }
    cout << a[1].val - a[n].val <<" "<< ans.size() << endl;
    for(int i = 0 ; i < ans.size() ;i ++)
        cout << ans[i].first <<" "<<ans[i].second << endl;

}


#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int M=1000000007;

signed main()
{
  fastio;
  int t=1;
  //cin>>t;

  while(t--)
  {
    int n,k,maxxxx=-M,minnnn=M;
    cin>>n>>k;
    vector <int> arr(n);
    vector <pii> p;

    for(int i=0;i<n;i++)
    {
      cin>>arr[i];
      maxxxx=max(maxxxx,arr[i]);
      minnnn=min(minnnn,arr[i]);
    }

    int maxx,minn,mini,maxi,temp,tempp;
    int count=0,doubt=0;

    while(count<k)
    {
      mini=min_element(arr.begin(),arr.end())-arr.begin();
      maxi=max_element(arr.begin(),arr.end())-arr.begin();
      temp=*max_element(arr.begin(),arr.end());
      tempp=*min_element(arr.begin(),arr.end());

      if(temp-tempp<=1)
        break;
      arr[mini]++;
      arr[maxi]--;
      count++;
        p.pb({maxi+1,mini+1});
    }

    cout<<*max_element(arr.begin(),arr.end())-*min_element(arr.begin(),arr.end())<<" "<<count<<"\n";
    for(auto x:p)
     cout<<x.first<<" "<<x.second<<"\n";
  }

  return 0;
}


#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int M=1000000007;

signed main()
{
  fastio;
  int t=1;
  //cin>>t;

  while(t--)
  {
  	int n,k,maxxxx=-M,minnnn=M;
  	cin>>n>>k;
    vector <int> arr(n);
    vector <pii> p;

    for(int i=0;i<n;i++)
    {
    	cin>>arr[i];
    	maxxxx=max(maxxxx,arr[i]);
    	minnnn=min(minnnn,arr[i]);
    }

    int minn= maxxxx-minnnn;
    int count=0;

    while(minn>0 and count<k)
    {
    	int mini=min_element(arr.begin(),arr.end())-arr.begin();
    	int maxi=max_element(arr.begin(),arr.end())-arr.begin();
    	int temp=*max_element(arr.begin(),arr.end());
    	int tempp=*min_element(arr.begin(),arr.end());

    	int diff=temp-tempp;
    	if(diff==0)
    		break;
    	arr[mini]++;
    	arr[maxi]--;
    	p.pb({maxi+1,mini+1});
    	count++;
    	minn=diff;
    }

    cout<<*max_element(arr.begin(),arr.end())-*min_element(arr.begin(),arr.end())<<" "<<count<<"\n";
    for(auto x:p)
     cout<<x.first<<" "<<x.second<<"\n";
  }

  return 0;
}


#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int M=1000000007;

signed main()
{
  fastio;
  int t=1;
  //cin>>t;

  while(t--)
  {
  	int n,k,maxxxx=-M,minnnn=M;
  	cin>>n>>k;
    vector <int> arr(n);
    vector <pii> p;

    for(int i=0;i<n;i++)
    {
    	cin>>arr[i];
    	maxxxx=max(maxxxx,arr[i]);
    	minnnn=min(minnnn,arr[i]);
    }

    int minn= maxxxx-minnnn;
    int count=0;

    while(minn>0 and count<k)
    {
    	int mini=min_element(arr.begin(),arr.end())-arr.begin();
    	int maxi=max_element(arr.begin(),arr.end())-arr.begin();
    	int temp=*max_element(arr.begin(),arr.end());
    	int tempp=*min_element(arr.begin(),arr.end());

    	int diff=temp-tempp;
    	if(minn<diff)
    		break;
    	arr[mini]++;
    	arr[maxi]--;
    	p.pb({maxi+1,mini+1});
    	count++;
    	minn=diff;
    }

    cout<<*max_element(arr.begin(),arr.end())-*min_element(arr.begin(),arr.end())<<" "<<count<<"\n";
    for(auto x:p)
     cout<<x.first<<" "<<x.second<<"\n";
  }

  return 0;
}


#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int M=1000000007;

signed main()
{
  fastio;
  int t=1;
  //cin>>t;

  while(t--)
  {
  	int n,k,maxxxx=-M,minnnn=M;
  	cin>>n>>k;
    vector <int> arr(n);
    vector <pii> p;

    for(int i=0;i<n;i++)
    {
    	cin>>arr[i];
    	maxxxx=max(maxxxx,arr[i]);
    	minnnn=min(minnnn,arr[i]);
    }

    int minn= M;
    int count=0;

    while(minn>0 and count<k)
    {
    	int mini=min_element(arr.begin(),arr.end())-arr.begin();
    	int maxi=max_element(arr.begin(),arr.end())-arr.begin();
    	int temp=*max_element(arr.begin(),arr.end());
    	int tempp=*min_element(arr.begin(),arr.end());

    	int diff=temp-tempp;
    	if(minn<diff)
    		break;
    	arr[mini]++;
    	arr[maxi]--;
    	p.pb({maxi+1,mini+1});
    	count++;
    	minn=diff;
    }

    cout<<*max_element(arr.begin(),arr.end())-*min_element(arr.begin(),arr.end())<<" "<<count<<"\n";
    for(auto x:p)
     cout<<x.first<<" "<<x.second<<"\n";
  }

  return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll          long long
#define pb          push_back
#define eb 			emplace_back
#define mp 			make_pair
#define pii         pair<ll,ll>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define fi          first
#define si          second
#define sz(x)       (ll)x.size()
#define endl        "\n"
#define checkbit(n, b) ((n >> b) & 1)
#define mod        1000000007
#define inf        1000000000000000000
#define puts(n)		cout<<n;
#define um           unordered_map<ll,ll>
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define setbits(x) __builtin_popcount(x)
#define mset(m,v) memset(m,v,sizeof(m))
#define int ll
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
using namespace std;
int power(int base, int exp);

//=======================

void solve() {
 int n,k;
 cin>>n>>k;
 vi arr(n,0);
 rep(i,0,n)
 cin>>arr[i];
 int h;

 vii ans;

while(k>=0)
 {

 	 int min=INT_MAX,i=-1,j=-1,max=INT_MIN;
 	rep(k,0,n)
 	{
 		if(arr[k]>max)
 		{
 			max=arr[k];
 			i=k;
 		}
 	  if(arr[k]<min)
 	  {
 	  	min=arr[k];
 	  	j=k;
 	  }
 	}
 	if(k==0)
 	{
 		h=max-min;
 		break;
 	}
 	if(max-min<=1)
 	{
 		h=max-min;
 		break;
 	}

 	ans.pb({i+1,j+1});
 	arr[i]--;
 	arr[j]++;
 	k--;
 }
 cout<<h<<" "<<sz(ans)<<endl;
 rep(i,0,sz(ans))
 {
 	cout<<ans[i].fi<<" "<<ans[i].si<<endl;
 }
}

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

   int t = 1;
   // cin >> t;
    while(t--) {
      solve();
    }

    return 0;
}

int power(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll          long long
#define pb          push_back
#define eb 			emplace_back
#define mp 			make_pair
#define pii         pair<ll,ll>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define fi          first
#define si          second
#define sz(x)       (ll)x.size()
#define endl        "\n"
#define checkbit(n, b) ((n >> b) & 1)
#define mod        1000000007
#define inf        1000000000000000000
#define puts(n)		cout<<n;
#define um           unordered_map<ll,ll>
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define setbits(x) __builtin_popcount(x)
#define mset(m,v) memset(m,v,sizeof(m))
#define int ll
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
using namespace std;
int power(int base, int exp);

//=======================

void solve() {
 int n,k;
 cin>>n>>k;
 vi arr(n,0);
 rep(i,0,n)
 {
 	cin>>arr[i];
 }
 int h=0;
 vii ans;
 while(k--)
 {
 	int max=0,i=0;
 	int min=INT_MAX,j=0;
 	rep(p,0,n)
 	{
 		if(arr[p]>max)
 		{
 			max=arr[p];
 			i=p;
 		}
 		if(arr[p]<min)
 		{
 			min=arr[p];
 			j=p;
 		}
 	}
 	if(max-min==h)
 	{
 		break;
 	}
 	h=max-min;
 	arr[i]--;
 	arr[j]++;
 	ans.pb({i+1,j+1});
 }
 sort(all(arr));
 h=arr[n-1]-arr[0];
 cout<<h<<" "<<sz(ans)<<endl;
 rep(i,0,sz(ans))
 {
 	cout<<ans[i].fi<<" "<<ans[i].si<<endl;
 }
}

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

   int t = 1;
    //cin >> t;
    while(t--) {
      solve();
    }

    return 0;
}

int power(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}
// Problem : B. Towers
// Contest : Codeforces - Codeforces Round #274 (Div. 2)
// URL : https://codeforces.com/contest/479/problem/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll          long long
#define pb          push_back
#define eb 			emplace_back
#define mp 			make_pair
#define pii         pair<ll,ll>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define fi          first
#define si          second
#define sz(x)       (ll)x.size()
#define endl        "\n"
#define checkbit(n, b) ((n >> b) & 1)
#define mod        1000000007
#define inf        1000000000000000000
#define puts(n)		cout<<n;
#define um           unordered_map<ll,ll>
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define setbits(x) __builtin_popcount(x)
#define mset(m,v) memset(m,v,sizeof(m))
#define int ll
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
using namespace std;
int power(int base, int exp);

//=======================

void solve() {
 int n,k;
 cin>>n>>k;
 vi arr(n,0);
 rep(i,0,n)
 {
 	cin>>arr[i];
 }
 int h=0;
 vii ans;
 set<int>s;
 while(k--)
 {
 	int max=0,i=0;
 	int min=INT_MAX,j=0;
 	rep(p,0,n)
 	{
 		if(arr[p]>max)
 		{
 			max=arr[p];
 			i=p;
 		}
 		if(arr[p]<min)
 		{
 			min=arr[p];
 			j=p;
 		}
 	}
 	h=max-min;
 	if(s.count(h))
 	{
 		break;
 	}
 s.insert(h);
 	arr[i]--;
 	arr[j]++;
 	ans.pb({i+1,j+1});
 }
 sort(all(arr));
 h=arr[n-1]-arr[0];
 cout<<h<<" "<<sz(ans)<<endl;
 rep(i,0,sz(ans))
 {
 	cout<<ans[i].fi<<" "<<ans[i].si<<endl;
 }
}

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

   int t = 1;
    //cin >> t;
    while(t--) {
      solve();
    }

    return 0;
}

int power(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}
// Problem : B. Towers
// Contest : Codeforces - Codeforces Round #274 (Div. 2)
// URL : https://codeforces.com/contest/479/problem/B
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll          long long
#define pb          push_back
#define eb 			emplace_back
#define mp 			make_pair
#define pii         pair<ll,ll>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define fi          first
#define si          second
#define sz(x)       (ll)x.size()
#define endl        "\n"
#define checkbit(n, b) ((n >> b) & 1)
#define mod        1000000007
#define inf        1000000000000000000
#define puts(n)		cout<<n;
#define um           unordered_map<ll,ll>
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define setbits(x) __builtin_popcount(x)
#define mset(m,v) memset(m,v,sizeof(m))
#define int ll
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
using namespace std;
int power(int base, int exp);

//=======================

void solve() {
 int n,k;
 cin>>n>>k;
 vi arr(n,0);
 rep(i,0,n)
 cin>>arr[i];
 int h;
 set<pii>s;
 vii ans;

while(k>=0)
 {

 	 int min=INT_MAX,i=0,j=0,max=INT_MIN;
 	rep(k,0,n)
 	{
 		if(arr[k]>max)
 		{
 			max=arr[k];
 			i=k;
 		}
 	  if(arr[k]<min)
 	  {
 	  	min=arr[k];
 	  	j=k;
 	  }
 	}
 	if(k==0)
 	{
 		h=max-min;
 		break;
 	}
 	if(max-min<=1)
 	{
 		h=max-min;
 		break;
 	}
 	if(s.count({max,min}))
 	{
 		h=max-min;
 	break;
 	}
 	s.insert({max,min});
 	h=max-min;
 	ans.pb({i+1,j+1});
 	arr[i]--;
 	arr[j]++;
 	k--;
 }
 cout<<h<<" "<<sz(ans)<<endl;
 rep(i,0,sz(ans))
 {
 	cout<<ans[i].fi<<" "<<ans[i].si<<endl;
 }
}

int32_t main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

   int t = 1;
   // cin >> t;
    while(t--) {
      solve();
    }

    return 0;
}

int power(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}


#include<bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>

const double pi=acos(-1.0);
//memset ( a , 0 , n * sizeof(ll) ) ;
using namespace std;
//using namespace __gnu_pbds;
#define   endl    '\n'
#define   sl(n)     scanf("%lld",&n)
#define   mp      make_pair
#define   pb      push_back
#define   ppb     pop_back
#define   fi      first
#define   se      second
#define   ll      long long
#define   ull     unsigned long long
#define   ld      long double
#define   pii     pair<int, int>
#define   f(i,a,b)  for(ll i = (ll)(a); i < (ll)(b); i++)
#define   rf(i,a,b)   for(ll i = (ll)(a); i > (ll)(b); i--)
#define   ms(a,b)   memset((a),(b),sizeof(a))
#define   abs(x)    ((x<0)?(-(x)):(x))
#define   MAX     1000005
#define   inf     LLONG_MAX
#define   ninf    LLONG_MIN
#define   MIN     INT_MIN
#define   yeet    return 0;
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long  MAX2(long long  a, long long  b){return (a)>(b)?(a):(b);}
inline long long  MAX3(long long  a, long long  b,long long  c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long  MIN2(long long  a, long long  b){return (a)<(b)?(a):(b);}
inline long long  MIN3(long long  a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}

//typedef
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;

int mod = 1e9 +7 ;
int64_t extGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {if (!a) {x = 0;y = 1;return b;}int64_t x1, y1;int64_t d = extGcd(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}

inline int addMod(int a, int b, int m = mod) {return ((int64_t)a + b) % m;}
inline int mulMod(int a, int b, int m = mod) {  return ((int64_t)a * b) % m;}
inline int divMod(int a, int b, int m = mod) {int64_t x, y;int64_t g = extGcd(b, m, x, y);if (g != 1) {cerr << "Bad gcd!" << endl;for (;;);}x = (x % m + m) % m;return mulMod(a, x, m);}

inline ll exp(ll a,ll b){if(a==0)return 0ll;ll r=1LL;while(b>0){if(b&1){r=r*(a%mod);r=(r+mod)%mod;}b/=2;a=(a%mod)*(a%mod);a=(a+mod)%mod;}return (r+mod)%mod;}
ll gcd(ll a,ll b){if(b==0)return a;if(a==0)return b;return gcd(b,a%b);}
uint32 setbits(ll n){uint32 count=0;while (n){n&=(n-1);count++;}return count; }
// ll f[MAX];
// ll iv[MAX];
// ll C(ll n,ll r){
// 	 if(n<r)return 0;
// 	 ll ans=(f[n]%mod * iv[r]%mod)%mod;
// 	 ans=(ans%mod * iv[n-r]%mod)%mod;
// 	 return ans%mod;
// }
// void init(){
// 	 f[0]=1;
//      iv[0]=1;
//      f(i,1,MAX)f[i]=(i*f[i-1])%mod;
//      iv[MAX-1]=exp(f[MAX-1],mod-2);

//      for(int i=MAX-2;i>=0;--i)iv[i]=(iv[i+1]*(i+1))%mod;

// }

///* seive */  const int N= 10000000;int lp[N+1];vector<int> pr;void seive(){for (int i=2; i<=N; ++i) {if (lp[i] == 0) {lp[i] = i;pr.push_back (i);}for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j) lp[i * pr[j]] = pr[j]; }}
////****************************************************************************************************************************************************************************************************************////
 int main(){
 	       int n,k;
 	       cin>>n>>k;
 	       ll a[n];
 	       f(i,0,n)cin>>a[i];
 	       vector<pair<int,int>>g1;
 	       int diff=INT_MAX;
 	       while(k--){
 	       	  int mx=INT_MIN;
 	       	  int mid=-1;
 	       	  int mn= INT_MAX;
 	       	  int nid=-1;
 	       	  for(int i=0;i<n;i++){
 	       	  	 if(a[i]>mx){mx=a[i];mid=i;}
 	       	  }
 	       	   for(int i=0;i<n;i++){
 	       	  	 if(a[i]<mn){mn=a[i];nid=i;}
 	       	  }
 	       	  a[mid]-=1;
 	       	  a[nid]+=1;
              g1.pb(mp(mid,nid));
              int Max= *(max_element(a,a+n));
              int Min=*(min_element(a,a+n));
               int curr= Max-Min;
               if(curr==0 or curr==diff){diff=curr;break;}
               if(curr<diff)diff=curr;
 	       }
 	       cout<<diff<<" "<<g1.size()<<endl;
 	       for(auto x:g1)cout<<x.fi+1<<" "<<x.se+1<<endl;

 	return 0;
 }


#include<bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>

const double pi=acos(-1.0);
//memset ( a , 0 , n * sizeof(ll) ) ;
using namespace std;
//using namespace __gnu_pbds;
#define   endl    '\n'
#define   sl(n)     scanf("%lld",&n)
#define   mp      make_pair
#define   pb      push_back
#define   ppb     pop_back
#define   fi      first
#define   se      second
#define   ll      long long
#define   ull     unsigned long long
#define   ld      long double
#define   pii     pair<int, int>
#define   f(i,a,b)  for(ll i = (ll)(a); i < (ll)(b); i++)
#define   rf(i,a,b)   for(ll i = (ll)(a); i > (ll)(b); i--)
#define   ms(a,b)   memset((a),(b),sizeof(a))
#define   abs(x)    ((x<0)?(-(x)):(x))
#define   MAX     1000005
#define   inf     LLONG_MAX
#define   ninf    LLONG_MIN
#define   MIN     INT_MIN
#define   yeet    return 0;
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long  MAX2(long long  a, long long  b){return (a)>(b)?(a):(b);}
inline long long  MAX3(long long  a, long long  b,long long  c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long  MIN2(long long  a, long long  b){return (a)<(b)?(a):(b);}
inline long long  MIN3(long long  a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}

//typedef
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;

int mod = 1e9 +7 ;
int64_t extGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {if (!a) {x = 0;y = 1;return b;}int64_t x1, y1;int64_t d = extGcd(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}

inline int addMod(int a, int b, int m = mod) {return ((int64_t)a + b) % m;}
inline int mulMod(int a, int b, int m = mod) {  return ((int64_t)a * b) % m;}
inline int divMod(int a, int b, int m = mod) {int64_t x, y;int64_t g = extGcd(b, m, x, y);if (g != 1) {cerr << "Bad gcd!" << endl;for (;;);}x = (x % m + m) % m;return mulMod(a, x, m);}

inline ll exp(ll a,ll b){if(a==0)return 0ll;ll r=1LL;while(b>0){if(b&1){r=r*(a%mod);r=(r+mod)%mod;}b/=2;a=(a%mod)*(a%mod);a=(a+mod)%mod;}return (r+mod)%mod;}
ll gcd(ll a,ll b){if(b==0)return a;if(a==0)return b;return gcd(b,a%b);}
uint32 setbits(ll n){uint32 count=0;while (n){n&=(n-1);count++;}return count; }
// ll f[MAX];
// ll iv[MAX];
// ll C(ll n,ll r){
// 	 if(n<r)return 0;
// 	 ll ans=(f[n]%mod * iv[r]%mod)%mod;
// 	 ans=(ans%mod * iv[n-r]%mod)%mod;
// 	 return ans%mod;
// }
// void init(){
// 	 f[0]=1;
//      iv[0]=1;
//      f(i,1,MAX)f[i]=(i*f[i-1])%mod;
//      iv[MAX-1]=exp(f[MAX-1],mod-2);

//      for(int i=MAX-2;i>=0;--i)iv[i]=(iv[i+1]*(i+1))%mod;

// }

///* seive */  const int N= 10000000;int lp[N+1];vector<int> pr;void seive(){for (int i=2; i<=N; ++i) {if (lp[i] == 0) {lp[i] = i;pr.push_back (i);}for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j) lp[i * pr[j]] = pr[j]; }}
////****************************************************************************************************************************************************************************************************************////
 int main(){
 	       int n,k;
 	       cin>>n>>k;
 	       ll a[n];
 	       f(i,0,n)cin>>a[i];
 	       vector<pair<int,int>>g1;
 	       int diff=INT_MAX;
 	       while(k--){
 	       	  int mx=INT_MIN;
 	       	  int mid=-1;
 	       	  int mn= INT_MAX;
 	       	  int nid=-1;
 	       	  for(int i=0;i<n;i++){
 	       	  	 if(a[i]>mx){mx=a[i];mid=i;}
 	       	  }
 	       	   for(int i=0;i<n;i++){
 	       	  	 if(a[i]<mn){mn=a[i];nid=i;}
 	       	  }
 	       	 if(mid==nid or a[mid]==a[nid]){ diff=0;break;}
 	       	 a[mid]-=1;
 	       	  a[nid]+=1;
              g1.pb(mp(mid,nid));
              int Max= *(max_element(a,a+n));
              int Min=*(min_element(a,a+n));
               int curr= Max-Min;
               if(curr==0 or curr==diff){diff=curr;break;}
               if(curr<diff)diff=curr;
 	       }
 	       cout<<diff<<" "<<g1.size()<<endl;
 	       for(auto x:g1)cout<<x.fi+1<<" "<<x.se+1<<endl;

 	return 0;
 }


#include<bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>

const double pi=acos(-1.0);
//memset ( a , 0 , n * sizeof(ll) ) ;
using namespace std;
//using namespace __gnu_pbds;
#define   endl    '\n'
#define   sl(n)     scanf("%lld",&n)
#define   mp      make_pair
#define   pb      push_back
#define   ppb     pop_back
#define   fi      first
#define   se      second
#define   ll      long long
#define   ull     unsigned long long
#define   ld      long double
#define   pii     pair<int, int>
#define   f(i,a,b)  for(ll i = (ll)(a); i < (ll)(b); i++)
#define   rf(i,a,b)   for(ll i = (ll)(a); i > (ll)(b); i--)
#define   ms(a,b)   memset((a),(b),sizeof(a))
#define   abs(x)    ((x<0)?(-(x)):(x))
#define   MAX     1000005
#define   inf     LLONG_MAX
#define   ninf    LLONG_MIN
#define   MIN     INT_MIN
#define   yeet    return 0;
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long  MAX2(long long  a, long long  b){return (a)>(b)?(a):(b);}
inline long long  MAX3(long long  a, long long  b,long long  c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long  MIN2(long long  a, long long  b){return (a)<(b)?(a):(b);}
inline long long  MIN3(long long  a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}

//typedef
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;

int mod = 1e9 +7 ;
int64_t extGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {if (!a) {x = 0;y = 1;return b;}int64_t x1, y1;int64_t d = extGcd(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}

inline int addMod(int a, int b, int m = mod) {return ((int64_t)a + b) % m;}
inline int mulMod(int a, int b, int m = mod) {  return ((int64_t)a * b) % m;}
inline int divMod(int a, int b, int m = mod) {int64_t x, y;int64_t g = extGcd(b, m, x, y);if (g != 1) {cerr << "Bad gcd!" << endl;for (;;);}x = (x % m + m) % m;return mulMod(a, x, m);}

inline ll exp(ll a,ll b){if(a==0)return 0ll;ll r=1LL;while(b>0){if(b&1){r=r*(a%mod);r=(r+mod)%mod;}b/=2;a=(a%mod)*(a%mod);a=(a+mod)%mod;}return (r+mod)%mod;}
ll gcd(ll a,ll b){if(b==0)return a;if(a==0)return b;return gcd(b,a%b);}
uint32 setbits(ll n){uint32 count=0;while (n){n&=(n-1);count++;}return count; }
// ll f[MAX];
// ll iv[MAX];
// ll C(ll n,ll r){
// 	 if(n<r)return 0;
// 	 ll ans=(f[n]%mod * iv[r]%mod)%mod;
// 	 ans=(ans%mod * iv[n-r]%mod)%mod;
// 	 return ans%mod;
// }
// void init(){
// 	 f[0]=1;
//      iv[0]=1;
//      f(i,1,MAX)f[i]=(i*f[i-1])%mod;
//      iv[MAX-1]=exp(f[MAX-1],mod-2);

//      for(int i=MAX-2;i>=0;--i)iv[i]=(iv[i+1]*(i+1))%mod;

// }

///* seive */  const int N= 10000000;int lp[N+1];vector<int> pr;void seive(){for (int i=2; i<=N; ++i) {if (lp[i] == 0) {lp[i] = i;pr.push_back (i);}for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j) lp[i * pr[j]] = pr[j]; }}
////****************************************************************************************************************************************************************************************************************////
 int main(){
 	       int n,k;
 	       cin>>n>>k;
 	       ll a[n];
 	       f(i,0,n)cin>>a[i];
 	       vector<pair<int,int>>g1;
 	       int diff=INT_MAX;
 	       while(k--){
 	       	  int mx=INT_MIN;
 	       	  int mid=-1;
 	       	  int mn= INT_MAX;
 	       	  int nid=-1;
 	       	  for(int i=0;i<n;i++){
 	       	  	 if(a[i]>mx){mx=a[i];mid=i;}
 	       	  }
 	       	   for(int i=0;i<n;i++){
 	       	  	 if(a[i]<mn){mn=a[i];nid=i;}
 	       	  }
 	       	 if(mid==nid or a[mid]==a[nid]){ diff=0;break;}
 	       	 a[mid]-=1;
 	       	  a[nid]+=1;
              g1.pb(mp(mid,nid));

 	       }
 	        diff= *(max_element(a,a+n)) - *(min_element(a,a+n));
 	       cout<<diff<<" "<<g1.size()<<endl;
 	       for(auto x:g1)cout<<x.fi+1<<" "<<x.se+1<<endl;

 	return 0;
 }


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

int main(){

  ios_base :: sync_with_stdio(false);
  cin.tie(NULL);

  int n,k;
  cin >> n >> k;

  vector <pair <int,int> > vet(n);

  for(int i=0;i<n;i++){
    cin >> vet[i].first;
    vet[i].second = i+1;
  }

  vector <pair <int,int> > ans(k);

  int op = 0;
  int dif = 1e9 + 7;

  sort(vet.begin(),vet.end());

  for(int i=0;i<k;i++){

    if(vet[n-1].first == vet[0].first){
      dif = 0;
      op = 0;
      break;
    }

    vet[0].first++;
    vet[n-1].first--;
    ans[i].first = vet[n-1].second;
    ans[i].second = vet[0].second;

    sort(vet.begin(),vet.end());

    if(dif > vet[n-1].first - vet[0].first){
      dif = vet[n-1].first - vet[0].first;
      op = i+1;
    }

  }

  cout << dif << " " << op << endl;

  for(int i=0;i<op;i++)
    cout << ans[i].first << " " << ans[i].second << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

int main(){

  ios_base :: sync_with_stdio(false);
  cin.tie(NULL);

  int n,k;
  cin >> n >> k;

  vector <pair <int,int> > vet(n);

  for(int i=0;i<n;i++){
    cin >> vet[i].first;
    vet[i].second = i+1;
  }

  vector <pair <int,int> > ans(k);

  for(int i=0;i<k;i++){
    sort(vet.begin(),vet.end());
    vet[0].first++;
    vet[n-1].first--;
    ans[i].first = vet[n-1].second;
    ans[i].second = vet[0].second;
  }

    sort(vet.begin(),vet.end());

  cout << vet[n-1].first - vet[0].first << " " << k << endl;

  for(int i=0;i<k;i++)
    cout << ans[i].first << " " << ans[i].second << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

int main(){

  ios_base :: sync_with_stdio(false);
  cin.tie(NULL);

  int n,k;
  cin >> n >> k;

  vector <pair <int,int> > vet(n);

  for(int i=0;i<n;i++){
    cin >> vet[i].first;
    vet[i].second = i+1;
  }

  vector <pair <int,int> > ans(k);

  int op = 0;
  int dif = 1e9 + 7;

  sort(vet.begin(),vet.end());

  if(vet[n-1].first == vet[0].first){
    cout << 0 << " " << 0 << endl;
    return 0;
  }

  for(int i=0;i<k;i++){
    vet[0].first++;
    vet[n-1].first--;
    ans[i].first = vet[n-1].second;
    ans[i].second = vet[0].second;

    sort(vet.begin(),vet.end());

    if(dif > vet[n-1].first - vet[0].first){
      dif = vet[n-1].first - vet[0].first;
      op = i+1;
    }

  }

  cout << dif << " " << op << endl;

  for(int i=0;i<op;i++)
    cout << ans[i].first << " " << ans[i].second << endl;

  return 0;
}


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

using ll = long long;

void bober () {
	int n, k;
	cin >> n >> k;
	set <pair <int, int>> s;
	for (int i=1; i<=n; i++) {
		int x;
		cin >> x;
		s.insert({x, i});
	}
	if (n == 1) {
		cout << (*s.begin()).first << " 0";
		return;
	}
	vector <pair <int, int>> ans;
	while (k--) {
		pair <int, int> mx = *s.rbegin(), mn = *s.begin();
		if (mx.first-mn.first == 1) break;
		s.erase(mx); s.erase(mn);
		mx.first --; mn.first ++;
		s.insert(mx); s.insert(mn);
		ans.push_back(pair(mx.second, mn.second));
	}
	cout << (*s.rbegin()).first - (*s.begin()).first << " " << ans.size() << "\n";
	for (pair <int, int> &i:ans) {
		cout << i.first << " " << i.second << "\n";
	}
}

int main () {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	q = 1; //cin >> q;
	while (q--) {
		bober ();
	}
	return 0;
}


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

using ll = long long;

void bober () {
	int n, k;
	cin >> n >> k;
	set <pair <int, int>> s;
	for (int i=1; i<=n; i++) {
		int x;
		cin >> x;
		s.insert({x, i});
	}
	if (n == 1) {
		cout << (*s.begin()).first << " 0";
		return;
	}
	vector <pair <int, int>> ans;
	while (k--) {
		pair <int, int> mx = *s.rbegin(), mn = *s.begin();
		if (mx.first-mn.first <= 1) break;
		s.erase(mx); s.erase(mn);
		mx.first --; mn.first ++;
		s.insert(mx); s.insert(mn);
		ans.push_back(pair(mx.second, mn.second));
	}
	cout << (*s.rbegin()).first - (*s.begin()).first << " " << ans.size() << "\n";
	for (pair <int, int> &i:ans) {
		cout << i.first << " " << i.second << "\n";
	}
}

int main () {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	q = 1; //cin >> q;
	while (q--) {
		bober ();
	}
	return 0;
}


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

using ll = long long;

void bober () {
	int n, k;
	cin >> n >> k;
	set <pair <int, int>> s;
	for (int i=1; i<=n; i++) {
		int x;
		cin >> x;
		s.insert({x, i});
	}
	if (n == 1) {
		cout << "0 0";
		return;
	}
	vector <pair <int, int>> ans;
	while (k--) {
		pair <int, int> mx = *s.rbegin(), mn = *s.begin();
		if (mx.first-mn.first <= 1) break;
		s.erase(mx); s.erase(mn);
		mx.first --; mn.first ++;
		s.insert(mx); s.insert(mn);
		ans.push_back(pair(mx.second, mn.second));
	}
	cout << (*s.rbegin()).first - (*s.begin()).first << " " << ans.size() << "\n";
	for (pair <int, int> &i:ans) {
		cout << i.first << " " << i.second << "\n";
	}
}

int main () {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int q;
	q = 1; //cin >> q;
	while (q--) {
		bober ();
	}
	return 0;
}


//#include <bits/stdc++.h>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<cstring>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<iterator>
#include<algorithm>
#include<bitset>

#define Filein(N) freopen("N", "r", stdin);
#define Fileout(N) freopen("N", "w", stdout);
#define sci(x) scanf("%d", &x)
#define sci2(x,y) scanf("%d%d", &x,&y)
#define scll2(x,y) scanf("%lld%lld",&x,&y)
#define scll(x) scanf("%lld",&x)
#define scd(x) scanf("%lf", &x)
#define scstr(x) scanf("%s", x)
#define pfi(x) printf("%d", x)
#define pfll(x) printf("%lld", x)
#define pfd(x) printf("%f", x)
#define pfstr(x) printf("%s", x)
#define ps printf(" ")
#define pn printf("\n")
#define pfdt printf("..")

#define For(var,start,end) for(int var=start; var<=end;var++)
#define rFor(var,start,end) for(int var=start; var>=end; var--)
#define TestCase int testcase, cs=1; cin>>testcase;while(testcase--)

#define mkp make_pair
#define pb push_back
#define fi first
#define se second

#define px(x) cout<<"X -> "<<x<<endl;
#define py(x) cout<<"Y = "<<x<<endl;
#define pz(x) cout<<"Z >> "<<x<<endl;
#define pstr(x) printf("%s\n",x);

using namespace std;
using ll = long long;
using vi = vector <int>;
using mpii = map<int,int>;
using si = set<int>;
using sll = set<ll>;
using vll= vector<ll>;

#define INF 1000000000
#define LINF 4000000000000000000
#define pii  (2*acos(0))
#define Mod 1000000007

 int main(){
  int n,k;
  sci2(n,k);
  int ar[n+5];
  For(i,0,n-1) sci(ar[i]);
  vi form,to;
  int con = k;

  ///implementation...

  while(k--){
  int mx=0,mn=INF,pi=0,pj=0;
  For(i,0,n-1){
   if(ar[i]>mx){
    mx = ar[i];
    pi = i;
   }
   if(ar[i]<mn){
    mn = ar[i];
    pj = i;
   }
  }

  int dif = (mx-mn)/2;

   if(dif>con) {
        int mdif = dif-con;
        dif -=mdif;
    }
   if(dif==0) break;
   else {
    ar[pi]-=dif;
    ar[pj]+=dif;

    For(z,1,dif){
     form.pb(pi+1);
     to.pb(pj+1);
    }

    con-=dif;
    if(con<1) break;

   }

  }

  ///output section...
 int mn = INF,mx=0;
  For(i,0,n-1){
   mx = max(mx,ar[i]);
   mn = min(mn,ar[i]);

  }
  pfi(mx-mn);ps;pfi(to.size());pn;

  for(int i=0;i<form.size();i++){
   pfi(form[i]);ps;pfi(to[i]);pn;
  }

    return 0;
}