/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lowb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs(a) < eps && abs(b) < eps) return 0;
	else if(abs1(a - b) / abs1(a) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename t>
inline int jud(t a, t b){
	if(a < b) return -1;
	if(a == b) return 0;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1) be--;
		if(!f_small && jud(*(a + be), val) == -1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1) be--;
		if(f_small && jud(*(a + be), val) == 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

const int maxn = 200010;
int dp[maxn][7];
int orig[maxn], arr[maxn][2], larr;
int stat[maxn];
int n;

inline pii adv(pii now, int add, int base = 5) {
	pii ans = now;
	if(ans.second >= base) {
		ans.first++;
		ans.second = 0;
		add--;
	}
	ans.second += add;
	ans.first += ans.second / base;
	ans.second %= base;
	return ans;
}

int main() {
	ios_base::sync_with_stdio(0);
//	freopen("input.txt", "r", stdin);
	scanf("%d", &n);
	if(n == 1) {
		printf("-1\n");
		return 0;
	}
	bool fla = 1;
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		stat[orig[i]]++;
		if(orig[i] != 0 && stat[orig[i]] > 5) fla = 0;
	}
	if(!fla) {
		printf("-1\n");
		return 0;
	}
	if(n > 100) {
		for(int i = 0; i < n; i++) if(orig[i] == 0) {
			printf("%d\n", i - 1);
			printf("%d ", orig[i - 1]);
			int j = i;
			for(; orig[j] == 0 && j != n; j++)
			printf("%d %d\n", j, orig[j]);
			i = j;
		}
	}

	arr[larr][0] = 0;
	arr[larr][1] = 0;
	larr++;
	for(int i = 0; i < n; i++) if(orig[i] != 0) {
		arr[larr][0] = orig[i];
		arr[larr++][1] = i + 1;
	}
	memset(dp, -1, sizeof(dp));
	dp[0][4] = 0;
	for(int i = 0; i < larr - 1; i++) {
		for(int j = 0; j < 5; j++) if(dp[arr[i][1]][j] != -1) {
//			cerr << 1 << endl;
			int l = arr[i + 1][1] - arr[i][1];
			pii big = adv(mpr(arr[i][0], j), l, 2);
//			cerr << "big: " << big.first << ' ' << big.second << endl;
			pii small = adv(mpr(arr[i][0], j), l, 5);
//			cerr << "small: " << small.first << ' ' << small.second <</ endl;
			for(int k = 0; k < 5; k++)
				if(mpr(arr[i + 1][0] + k / 2, k % 2) >= small && mpr(arr[i + 1][0] + k / 2, k % 2) <= big)
					dp[arr[i + 1][1]][k] = j;
		}
	}
//	for(int i = 0; i < larr; i++) {
//		cerr << arr[i][0] << ' ' << arr[i][1] << endl;
//		for(int j = 0; j < 5; j++) {
//			cerr << dp[arr[i][1]][j] << ' ';
//		}
//		cerr << endl;
//	}

	int f = -1;
	for(int j = 4; j >= 0; j--) if(dp[arr[larr - 1][1]][j] != -1) {
		if(j == 0 && arr[larr - 1][1] == n)
			break;
		f = j;
		break;
	}
	if(f == -1) {
		printf("-1\n");
		return 0;
	}
	int be = arr[larr - 1][1];
	if(f == 0) {
		orig[be] = arr[larr - 1][0];
		be++;
	}
//	cerr << f << endl;
	int flag = 0;
	for(int i = be, add = arr[larr - 1][0] + 1; i < n; i++, flag ^= 1) {
		orig[i] = add;
		if(flag) add++;
	}
	if(flag)
		orig[n - 1] = orig[n - 2];
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	for(int i = larr - 1; i; i--) {
//		cerr << "f: " << f << endl;
		int nxt = dp[arr[i][1]][f];
		pii be = mpr(arr[i - 1][0], nxt);
		for(int j = arr[i - 1][1]; j < arr[i][1] - 1; j++) {
			be.second++;
//			pii nxt = adv(be, arr[i ][1] - 1 - j, 2);
//			cerr << arr[i][1] - 1 - j << endl;
//			cerr << "be: " << be.first << ' ' << be.second << endl;
//			cerr << "nxt: " << nxt.first << ' ' << nxt.second << endl;
//			cerr << f + max(2 * (arr[i][0] - be.first - 1), 0) << endl;
			if(be.first != arr[i][0] && (be.second == 5 || adv(be, arr[i][1] - 1 - j, 2) < mpr(arr[i][0], f) ||
			arr[i][1] - 1 - j <= f + 2 * (arr[i][0] - be.first - 1)))
				be.first++, be.second = 0;
			orig[j] = be.first;
		}
		f = nxt;
//		cerr << endl;
	}
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	printf("%d\n", orig[n - 1]);
	for(int i = 0; i < n; i++) printf("%d ", orig[i]);
	printf("\n");

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lowb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs(a) < eps && abs(b) < eps) return 0;
	else if(abs1(a - b) / abs1(a) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename t>
inline int jud(t a, t b){
	if(a < b) return -1;
	if(a == b) return 0;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1) be--;
		if(!f_small && jud(*(a + be), val) == -1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1) be--;
		if(f_small && jud(*(a + be), val) == 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

const int maxn = 200010;
int dp[maxn][7];
int orig[maxn], arr[maxn][2], larr;
int stat[maxn];
int n;

inline pii adv(pii now, int add, int base = 5) {
	pii ans = now;
	if(ans.second >= base) {
		ans.first++;
		ans.second = 0;
		add--;
	}
	ans.second += add;
	ans.first += ans.second / base;
	ans.second %= base;
	return ans;
}

int main() {
	ios_base::sync_with_stdio(0);
	freopen("input.txt", "r", stdin);
	scanf("%d", &n);
	if(n == 1) {
		printf("-1\n");
		return 0;
	}
	bool fla = 1;
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		stat[orig[i]]++;
		if(orig[i] != 0 && stat[orig[i]] > 5) fla = 0;
	}
	if(!fla) {
		printf("-1\n");
		return 0;
	}
	if(n > 100) {
		for(int i = 0; i < n; i++) if(orig[i] == 0) {
			printf("%d\n", i - 1);
			printf("%d ", orig[i - 1]);
			int j = i;
			for(; orig[j] == 0 && j != n; j++);
			printf("%d %d\n", j, orig[j]);
			i = j;
		}
	}

	arr[larr][0] = 0;
	arr[larr][1] = 0;
	larr++;
	for(int i = 0; i < n; i++) if(orig[i] != 0) {
		arr[larr][0] = orig[i];
		arr[larr++][1] = i + 1;
	}
	memset(dp, -1, sizeof(dp));
	dp[0][4] = 0;
	for(int i = 0; i < larr - 1; i++) {
		for(int j = 0; j < 5; j++) if(dp[arr[i][1]][j] != -1) {
//			cerr << 1 << endl;
			int l = arr[i + 1][1] - arr[i][1];
			pii big = adv(mpr(arr[i][0], j), l, 2);
//			cerr << "big: " << big.first << ' ' << big.second << endl;
			pii small = adv(mpr(arr[i][0], j), l, 5);
//			cerr << "small: " << small.first << ' ' << small.second <</ endl;
			for(int k = 0; k < 5; k++)
				if(mpr(arr[i + 1][0] + k / 2, k % 2) >= small && mpr(arr[i + 1][0] + k / 2, k % 2) <= big)
					dp[arr[i + 1][1]][k] = j;
		}
	}
//	for(int i = 0; i < larr; i++) {
//		cerr << arr[i][0] << ' ' << arr[i][1] << endl;
//		for(int j = 0; j < 5; j++) {
//			cerr << dp[arr[i][1]][j] << ' ';
//		}
//		cerr << endl;
//	}

	int f = -1;
	for(int j = 4; j >= 0; j--) if(dp[arr[larr - 1][1]][j] != -1) {
		if(j == 0 && arr[larr - 1][1] == n)
			break;
		f = j;
		break;
	}
	if(f == -1) {
		printf("-1\n");
		return 0;
	}
	int be = arr[larr - 1][1];
	if(f == 0) {
		orig[be] = arr[larr - 1][0];
		be++;
	}
//	cerr << f << endl;
	int flag = 0;
	for(int i = be, add = arr[larr - 1][0] + 1; i < n; i++, flag ^= 1) {
		orig[i] = add;
		if(flag) add++;
	}
	if(flag)
		orig[n - 1] = orig[n - 2];
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	for(int i = larr - 1; i; i--) {
//		cerr << "f: " << f << endl;
		int nxt = dp[arr[i][1]][f];
		pii be = mpr(arr[i - 1][0], nxt);
		for(int j = arr[i - 1][1]; j < arr[i][1] - 1; j++) {
			be.second++;
//			pii nxt = adv(be, arr[i ][1] - 1 - j, 2);
//			cerr << arr[i][1] - 1 - j << endl;
//			cerr << "be: " << be.first << ' ' << be.second << endl;
//			cerr << "nxt: " << nxt.first << ' ' << nxt.second << endl;
//			cerr << f + max(2 * (arr[i][0] - be.first - 1), 0) << endl;
			if(be.first != arr[i][0] && (be.second == 5 || adv(be, arr[i][1] - 1 - j, 2) < mpr(arr[i][0], f) ||
			arr[i][1] - 1 - j <= f + 2 * (arr[i][0] - be.first - 1)))
				be.first++, be.second = 0;
			orig[j] = be.first;
		}
		f = nxt;
//		cerr << endl;
	}
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	printf("%d\n", orig[n - 1]);
	for(int i = 0; i < n; i++) printf("%d ", orig[i]);
	printf("\n");

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lowb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs(a) < eps && abs(b) < eps) return 0;
	else if(abs1(a - b) / abs1(a) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename t>
inline int jud(t a, t b){
	if(a < b) return -1;
	if(a == b) return 0;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1) be--;
		if(!f_small && jud(*(a + be), val) == -1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1) be--;
		if(f_small && jud(*(a + be), val) == 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

const int maxn = 200010;
int dp[maxn][7];
int orig[maxn], arr[maxn][2], larr;
int n;

inline pii adv(pii now, int add, int base = 5) {
	pii ans = now;
	if(ans.second >= base) {
		ans.first++;
		ans.second = 0;
		add--;
	}
	ans.second += add;
	ans.first += ans.second / base;
	ans.second %= base;
	return ans;
}

int main() {
	ios_base::sync_with_stdio(0);
//	freopen("input.txt", "r", stdin);
	scanf("%d", &n);
	if(n == 1) {
		printf("-1\n");
		return 0;
	}
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
	}
	arr[larr][0] = 0;
	arr[larr][1] = 0;
	larr++;
	for(int i = 0; i < n; i++) if(orig[i] != 0) {
		arr[larr][0] = orig[i];
		arr[larr++][1] = i + 1;
	}
	memset(dp, -1, sizeof(dp));
	dp[0][4] = 0;
	for(int i = 0; i < larr - 1; i++) {
		for(int j = 0; j < 5; j++) if(dp[arr[i][1]][j] != -1) {
//			cerr << 1 << endl;
			int l = arr[i + 1][1] - arr[i][1];
			pii big = adv(mpr(arr[i][0], j), l, 2);
//			cerr << "big: " << big.first << ' ' << big.second << endl;
			pii small = adv(mpr(arr[i][0], j), l, 5);
//			cerr << "small: " << small.first << ' ' << small.second <</ endl;
			for(int k = 0; k < 5; k++)
				if(mpr(arr[i + 1][0] + k / 2, k % 2) >= small && mpr(arr[i + 1][0] + k / 2, k % 2) <= big)
					dp[arr[i + 1][1]][k] = j;
		}
	}
//	for(int i = 0; i < larr; i++) {
//		cerr << arr[i][0] << ' ' << arr[i][1] << endl;
//		for(int j = 0; j < 5; j++) {
//			cerr << dp[arr[i][1]][j] << ' ';
//		}
//		cerr << endl;
//	}

	int f = -1;
	for(int j = 4; j >= 0; j--) if(dp[arr[larr - 1][1]][j] != -1) {
		if(j == 0 && arr[larr - 1][1] == n)
			break;
		f = j;
		break;
	}
	if(f == -1) {
		printf("-1\n");
		return 0;
	}
	int be = arr[larr - 1][1];
	if(f == 0) {
		orig[be] = arr[larr - 1][0];
		be++;
	}
//	cerr << f << endl;
	int flag = 0;
	for(int i = be, add = arr[larr - 1][0] + 1; i < n; i++, flag ^= 1) {
		orig[i] = add;
		if(flag) add++;
	}
	if(flag)
		orig[n - 1] = orig[n - 2];
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	for(int i = larr - 1; i; i--) {
//		cerr << "f: " << f << endl;
		int nxt = dp[arr[i][1]][f];
		pii be = mpr(arr[i - 1][0], nxt);
		for(int j = arr[i - 1][1]; j < arr[i][1] - 1; j++) {
			be.second++;
//			pii nxt = adv(be, arr[i ][1] - 1 - j, 2);
//			cerr << arr[i][1] - 1 - j << endl;
//			cerr << "be: " << be.first << ' ' << be.second << endl;
//			cerr << "nxt: " << nxt.first << ' ' << nxt.second << endl;
//			cerr << f + max(2 * (arr[i][0] - be.first - 1), 0) << endl;
			if(be.first != arr[i][0] && (be.second == 5 || adv(be, arr[i][1] - 1 - j, 2) < mpr(arr[i][0], f) ||
			arr[i][1] - 1 - j <= f + 2 * (arr[i][0] - be.first - 1)))
				be.first++, be.second = 0;
			orig[j] = be.first;
		}
		f = nxt;
//		cerr << endl;
	}
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	printf("%d\n", orig[n - 1]);
	for(int i = 0; i < n; i++) printf("%d ", orig[i]);
	printf("\n");

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lowb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs(a) < eps && abs(b) < eps) return 0;
	else if(abs1(a - b) / abs1(a) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename t>
inline int jud(t a, t b){
	if(a < b) return -1;
	if(a == b) return 0;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1) be--;
		if(!f_small && jud(*(a + be), val) == -1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1) be--;
		if(f_small && jud(*(a + be), val) == 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

const int maxn = 200010;
int dp[maxn][7];
int orig[maxn], arr[maxn][2], larr;
int n;

inline pii adv(pii now, int add, int base = 5) {
	pii ans = now;
	if(ans.second >= base) {
		ans.first++;
		ans.second = 0;
		add--;
	}
	ans.second += add;
	ans.first += ans.second / base;
	ans.second %= base;
	return ans;
}

int main() {
	ios_base::sync_with_stdio(0);
//	freopen("input.txt", "r", stdin);
	scanf("%d", &n);
	if(n == 1) {
		printf("-1\n");
		return 0;
	}
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
	}
	arr[larr][0] = 0;
	arr[larr][1] = 0;
	larr++;
	for(int i = 0; i < n; i++) if(orig[i] != 0) {
		arr[larr][0] = orig[i];
		arr[larr++][1] = i + 1;
	}
	memset(dp, -1, sizeof(dp));
	dp[0][4] = 0;
	for(int i = 0; i < larr - 1; i++) {
		for(int j = 0; j < 5; j++) if(dp[arr[i][1]][j] != -1) {
//			cerr << 1 << endl;
			int l = arr[i + 1][1] - arr[i][1];
			pii big = adv(mpr(arr[i][0], j), l, 2);
//			cerr << "big: " << big.first << ' ' << big.second << endl;
			pii small = adv(mpr(arr[i][0], j), l, 5);
//			cerr << "small: " << small.first << ' ' << small.second << endl;
			for(int k = 0; k < 5; k++)
				if(mpr(arr[i + 1][0], k) >= small && mpr(arr[i + 1][0], k) <= big &&
				(arr[i + 1][0] == arr[i][0] || arr[i + 1][1] - arr[i][1] > k))
					dp[arr[i + 1][1]][k] = j;
		}
	}
//	for(int i = 0; i < larr; i++) {
//		cerr << arr[i][0] << ' ' << arr[i][1] << endl;
//		for(int j = 0; j < 5; j++) {
//			cerr << dp[arr[i][1]][j] << ' ';
//		}
//		cerr << endl;
//	}

	int f = -1;
	for(int j = 4; j >= 0; j--) if(dp[arr[larr - 1][1]][j] != -1) {
		if(j == 0 && arr[larr - 1][1] == n)
			break;
		f = j;
		break;
	}
	if(f == -1) {
		printf("-1\n");
		return 0;
	}
	int be = arr[larr - 1][1];
	if(f == 0) {
		orig[be] = arr[larr - 1][0];
		be++;
	}
//	cerr << f << endl;
	int flag = 0;
	for(int i = be, add = arr[larr - 1][0] + 1; i < n; i++, flag ^= 1) {
		orig[i] = add;
		if(flag) add++;
	}
	if(flag)
		orig[n - 1] = orig[n - 2];
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	for(int i = larr - 1; i; i--) {
//		cerr << "f: " << f << endl;
		int nxt = dp[arr[i][1]][f];
		pii be = mpr(arr[i - 1][0], nxt);
		for(int j = arr[i - 1][1]; j < arr[i][1] - 1; j++) {
			be.second++;
//			pii nxt = adv(be, arr[i ][1] - 1 - j, 2);
//			cerr << arr[i][1] - 1 - j << endl;
//			cerr << "be: " << be.first << ' ' << be.second << endl;
//			cerr << "nxt: " << nxt.first << ' ' << nxt.second << endl;
//			cerr << f + max(2 * (arr[i][0] - be.first - 1), 0) << endl;
			if(be.first != arr[i][0] && (be.second == 5 || adv(be, arr[i][1] - 1 - j, 2) < mpr(arr[i][0], f) ||
			arr[i][1] - 1 - j <= f + 2 * (arr[i][0] - be.first - 1)))
				be.first++, be.second = 0;
			orig[j] = be.first;
		}
		f = nxt;
//		cerr << endl;
	}
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	printf("%d\n", orig[n - 1]);
	for(int i = 0; i < n; i++) printf("%d ", orig[i]);
	printf("\n");

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lowb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs(a) < eps && abs(b) < eps) return 0;
	else if(abs1(a - b) / abs1(a) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename t>
inline int jud(t a, t b){
	if(a < b) return -1;
	if(a == b) return 0;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1) be--;
		if(!f_small && jud(*(a + be), val) == -1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1) be--;
		if(f_small && jud(*(a + be), val) == 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

const int maxn = 200010;
int dp[maxn][7];
int orig[maxn], arr[maxn][2], larr;
int stat[maxn];
int n;

inline pii adv(pii now, int add, int base = 5) {
	pii ans = now;
	if(ans.second >= base) {
		ans.first++;
		ans.second = 0;
		add--;
	}
	ans.second += add;
	ans.first += ans.second / base;
	ans.second %= base;
	return ans;
}
inline bool judge(pii a, pii b, int l) {
	int small = (b.first - a.first - 1) * 2 + b.second + 1 + int(a.second == 0);
	int big = (b.first - a.first - 1) * 5 + b.second + 5 - a.second;
	return l >= small && l <= big;
}

int main() {
	ios_base::sync_with_stdio(0);
//	freopen("input.txt", "r", stdin);
	scanf("%d", &n);
	if(n == 1) {
		printf("-1\n");
		return 0;
	}
	bool fla = 1;
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		stat[orig[i]]++;
		if(orig[i] != 0 && stat[orig[i]] > 5) fla = 0;
	}
	if(!fla) {
		printf("-1\n");
		return 0;
	}
//	if(n > 100) {
//		for(int i = 0; i < n; i++) if(orig[i] == 0) {
//			printf("%d\n", i - 1);
//			printf("%d ", orig[i - 1]);
//			int j = i;
//			for(; orig[j] == 0 && j != n; j++);
//			printf("%d %d\n", j, orig[j]);
//			i = j;
//		}
//	}

	arr[larr][0] = 0;
	arr[larr][1] = 0;
	larr++;
	for(int i = 0; i < n; i++) if(orig[i] != 0) {
		arr[larr][0] = orig[i];
		arr[larr++][1] = i + 1;
	}
	memset(dp, -1, sizeof(dp));
	dp[0][4] = 0;
	for(int i = 0; i < larr - 1; i++) {
//		if(arr[i][0] == 701)
//			cout << i << endl;
		for(int j = 0; j < 5; j++) if(dp[arr[i][1]][j] != -1) {
//			cerr << 1 << endl;
			int l = arr[i + 1][1] - arr[i][1];
//			pii big = adv(mpr(arr[i][0], j), l, 2);
//			pii small = adv(mpr(arr[i][0], j), l, 5);
//			if(arr[i][0] == 701) {
//				cout << "big: " << big.first << ' ' << big.second << endl;
//				cout << "small: " << small.first << ' ' << small.second << endl;
//			}
			if(arr[i + 1][0] == arr[i][0]) {
				if(j + arr[i + 1][1] - arr[i][1] < 5)
					dp[arr[i + 1][1]][j + arr[i + 1][1] - arr[i][1]] = j;
			} else for(int k = 0; k < 5; k++) {
				if(judge(mpr(arr[i][0], j), mpr(arr[i + 1][0], k), l))
					dp[arr[i + 1][1]][k] = j;
			}
		}
	}
//	for(int i = 0; i < larr; i++) if(arr[i][0] == 701){
//		cout << arr[i][0] << ' ' << arr[i][1] << endl;
//		for(int j = 0; j < 5; j++) {
//			cout << dp[arr[i][1]][j] << ' ';
//		}
//		cout << endl;
//	}

	int f = -1;
	if(arr[larr - 1][1] != n) {
		for(int j = 0; j <= 4; j++) if(dp[arr[larr - 1][1]][j] != -1) {
			if(j == 0 && arr[larr - 1][1] == n)
				break;
			f = j;
			break;
		}
	} else {
		for(int j = 1; j <= 4; j++) if(dp[arr[larr - 1][1]][j] != -1) {
			if(j == 0 && arr[larr - 1][1] == n)
				break;
			f = j;
			break;
		}
	}
	if(f == -1 || (f == 4 && arr[larr - 1][1] == n - 1)) {
		printf("-1\n");
		return 0;
	}
	int be = arr[larr - 1][1];
	if(f == 0) {
		orig[be] = arr[larr - 1][0];
		be++;
	}
//	cerr << f << endl;
	int flag = 0;
	for(int i = be, add = arr[larr - 1][0] + 1; i < n; i++, flag ^= 1) {
		orig[i] = add;
		if(flag) add++;
	}
	if(flag)
		orig[n - 1] = orig[n - 2];
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	for(int i = larr - 1; i; i--) {
//		cerr << "f: " << f << endl;
//		if(arr[i][0] == 1925) cout << f << ' ' << i << endl;
		int nxt = dp[arr[i][1]][f];
		pii be = mpr(arr[i - 1][0], nxt);
		for(int j = arr[i - 1][1]; j < arr[i][1] - 1; j++) {
			be.second++;
//			pii nxt = adv(be, arr[i ][1] - 1 - j, 2);
//			cerr << arr[i][1] - 1 - j << endl;
//			cerr << "be: " << be.first << ' ' << be.second << endl;
//			cerr << "nxt: " << nxt.first << ' ' << nxt.second << endl;
//			cerr << f + max(2 * (arr[i][0] - be.first - 1), 0) << endl;
			if(be.first != arr[i][0] && (be.second == 5 || adv(be, arr[i][1] - 1 - j, 2) < mpr(arr[i][0], f) ||
			arr[i][1] - 1 - j <= f + 2 * (arr[i][0] - be.first - 1)))
				be.first++, be.second = 0;
			orig[j] = be.first;
		}
		f = nxt;
//		cerr << endl;
	}
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	printf("%d\n", orig[n - 1]);
	for(int i = 0; i < n; i++) printf("%d ", orig[i]);
	printf("\n");

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lowb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs(a) < eps && abs(b) < eps) return 0;
	else if(abs1(a - b) / abs1(a) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename t>
inline int jud(t a, t b){
	if(a < b) return -1;
	if(a == b) return 0;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1) be--;
		if(!f_small && jud(*(a + be), val) == -1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1) be--;
		if(f_small && jud(*(a + be), val) == 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

const int maxn = 200010;
int dp[maxn][7];
int orig[maxn], arr[maxn][2], larr;
int stat[maxn];
int n;

inline pii adv(pii now, int add, int base = 5) {
	pii ans = now;
	if(ans.second >= base) {
		ans.first++;
		ans.second = 0;
		add--;
	}
	ans.second += add;
	ans.first += ans.second / base;
	ans.second %= base;
	return ans;
}

int main() {
	ios_base::sync_with_stdio(0);
//	freopen("input.txt", "r", stdin);
	scanf("%d", &n);
	if(n == 1) {
		printf("-1\n");
		return 0;
	}
	bool fla = 1;
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		stat[orig[i]]++;
		if(orig[i] != 0 && stat[orig[i]] > 5) fla = 0;
	}
	if(!fla) {
		printf("-1\n");
		return 0;
	}
	if(n > 100) {
		for(int i = 0; i < n; i++) if(orig[i] == 0) {
			printf("%d\n", i - 1);
			printf("%d ", orig[i - 1]);
			int j = i;
			for(; orig[j] == 0 && j != n; j++);
			printf("%d %d\n", j, orig[j]);
			i = j;
		}
	}

	arr[larr][0] = 0;
	arr[larr][1] = 0;
	larr++;
	for(int i = 0; i < n; i++) if(orig[i] != 0) {
		arr[larr][0] = orig[i];
		arr[larr++][1] = i + 1;
	}
	memset(dp, -1, sizeof(dp));
	dp[0][4] = 0;
	for(int i = 0; i < larr - 1; i++) {
		for(int j = 0; j < 5; j++) if(dp[arr[i][1]][j] != -1) {
//			cerr << 1 << endl;
			int l = arr[i + 1][1] - arr[i][1];
			pii big = adv(mpr(arr[i][0], j), l, 2);
//			cerr << "big: " << big.first << ' ' << big.second << endl;
			pii small = adv(mpr(arr[i][0], j), l, 5);
//			cerr << "small: " << small.first << ' ' << small.second <</ endl;
			for(int k = 0; k < 5; k++)
				if(mpr(arr[i + 1][0] + k / 2, k % 2) >= small && mpr(arr[i + 1][0] + k / 2, k % 2) <= big)
					dp[arr[i + 1][1]][k] = j;
		}
	}
//	for(int i = 0; i < larr; i++) {
//		cerr << arr[i][0] << ' ' << arr[i][1] << endl;
//		for(int j = 0; j < 5; j++) {
//			cerr << dp[arr[i][1]][j] << ' ';
//		}
//		cerr << endl;
//	}

	int f = -1;
	for(int j = 4; j >= 0; j--) if(dp[arr[larr - 1][1]][j] != -1) {
		if(j == 0 && arr[larr - 1][1] == n)
			break;
		f = j;
		break;
	}
	if(f == -1) {
		printf("-1\n");
		return 0;
	}
	int be = arr[larr - 1][1];
	if(f == 0) {
		orig[be] = arr[larr - 1][0];
		be++;
	}
//	cerr << f << endl;
	int flag = 0;
	for(int i = be, add = arr[larr - 1][0] + 1; i < n; i++, flag ^= 1) {
		orig[i] = add;
		if(flag) add++;
	}
	if(flag)
		orig[n - 1] = orig[n - 2];
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	for(int i = larr - 1; i; i--) {
//		cerr << "f: " << f << endl;
		int nxt = dp[arr[i][1]][f];
		pii be = mpr(arr[i - 1][0], nxt);
		for(int j = arr[i - 1][1]; j < arr[i][1] - 1; j++) {
			be.second++;
//			pii nxt = adv(be, arr[i ][1] - 1 - j, 2);
//			cerr << arr[i][1] - 1 - j << endl;
//			cerr << "be: " << be.first << ' ' << be.second << endl;
//			cerr << "nxt: " << nxt.first << ' ' << nxt.second << endl;
//			cerr << f + max(2 * (arr[i][0] - be.first - 1), 0) << endl;
			if(be.first != arr[i][0] && (be.second == 5 || adv(be, arr[i][1] - 1 - j, 2) < mpr(arr[i][0], f) ||
			arr[i][1] - 1 - j <= f + 2 * (arr[i][0] - be.first - 1)))
				be.first++, be.second = 0;
			orig[j] = be.first;
		}
		f = nxt;
//		cerr << endl;
	}
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	printf("%d\n", orig[n - 1]);
	for(int i = 0; i < n; i++) printf("%d ", orig[i]);
	printf("\n");

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lowb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs(a) < eps && abs(b) < eps) return 0;
	else if(abs1(a - b) / abs1(a) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename t>
inline int jud(t a, t b){
	if(a < b) return -1;
	if(a == b) return 0;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1) be--;
		if(!f_small && jud(*(a + be), val) == -1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1) be--;
		if(f_small && jud(*(a + be), val) == 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

const int maxn = 200010;
int dp[maxn][7];
int orig[maxn], arr[maxn][2], larr;
int stat[maxn];
int n;

inline pii adv(pii now, int add, int base = 5) {
	pii ans = now;
	if(ans.second >= base) {
		ans.first++;
		ans.second = 0;
		add--;
	}
	ans.second += add;
	ans.first += ans.second / base;
	ans.second %= base;
	return ans;
}

int main() {
	ios_base::sync_with_stdio(0);
//	freopen("input.txt", "r", stdin);
	scanf("%d", &n);
	if(n == 1) {
		printf("-1\n");
		return 0;
	}
	bool fla = 1;
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		stat[orig[i]]++;
		if(orig[i] != 0 && stat[orig[i]] > 5) fla = 0;
	}
	if(!fla) {
		printf("-1\n");
		return 0;
	}
//	if(n > 100) {
//		for(int i = 0; i < n; i++) if(orig[i] == 0) {
//			printf("%d\n", i - 1);
//			printf("%d ", orig[i - 1]);
//			int j = i;
//			for(; orig[j] == 0 && j != n; j++);
//			printf("%d %d\n", j, orig[j]);
//			i = j;
//		}
//	}

	arr[larr][0] = 0;
	arr[larr][1] = 0;
	larr++;
	for(int i = 0; i < n; i++) if(orig[i] != 0) {
		arr[larr][0] = orig[i];
		arr[larr++][1] = i + 1;
	}
	memset(dp, -1, sizeof(dp));
	dp[0][4] = 0;
	for(int i = 0; i < larr - 1; i++) {
//		if(arr[i][0] == 1925)
//			cout << i << endl;
		for(int j = 0; j < 5; j++) if(dp[arr[i][1]][j] != -1) {
//			cerr << 1 << endl;
			int l = arr[i + 1][1] - arr[i][1];
			pii big = adv(mpr(arr[i][0], j), l, 2);
			pii small = adv(mpr(arr[i][0], j), l, 5);
//			if(arr[i][0] == 1925) {
//				cout << "big: " << big.first << ' ' << big.second << endl;
//				cout << "small: " << small.first << ' ' << small.second << endl;
//			}
			if(arr[i + 1][0] == arr[i][0]) {
				if(j + arr[i + 1][1] - arr[i][1] < 5)
					dp[arr[i + 1][1]][j + arr[i + 1][1] - arr[i][1]] = 1;
			} else for(int k = 0; k < 5; k++)
				if(mpr(arr[i + 1][0] + k / 2, k % 2) >= small && mpr(arr[i + 1][0] + k / 2, k % 2) <= big)
					dp[arr[i + 1][1]][k] = j;
		}
	}
//	for(int i = 0; i < larr; i++) if(arr[i][0] == 1925){
//		cout << arr[i][0] << ' ' << arr[i][1] << endl;
//		for(int j = 0; j < 5; j++) {
//			cout << dp[arr[i][1]][j] << ' ';
//		}
//		cout << endl;
//	}

	int f = -1;
	for(int j = 4; j >= 0; j--) if(dp[arr[larr - 1][1]][j] != -1) {
		if(j == 0 && arr[larr - 1][1] == n)
			break;
		f = j;
		break;
	}
	if(f == -1) {
		printf("-1\n");
		return 0;
	}
	int be = arr[larr - 1][1];
	if(f == 0) {
		orig[be] = arr[larr - 1][0];
		be++;
	}
//	cerr << f << endl;
	int flag = 0;
	for(int i = be, add = arr[larr - 1][0] + 1; i < n; i++, flag ^= 1) {
		orig[i] = add;
		if(flag) add++;
	}
	if(flag)
		orig[n - 1] = orig[n - 2];
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	for(int i = larr - 1; i; i--) {
//		cerr << "f: " << f << endl;
//		if(arr[i][0] == 1925) cout << f << ' ' << i << endl;
		int nxt = dp[arr[i][1]][f];
		pii be = mpr(arr[i - 1][0], nxt);
		for(int j = arr[i - 1][1]; j < arr[i][1] - 1; j++) {
			be.second++;
//			pii nxt = adv(be, arr[i ][1] - 1 - j, 2);
//			cerr << arr[i][1] - 1 - j << endl;
//			cerr << "be: " << be.first << ' ' << be.second << endl;
//			cerr << "nxt: " << nxt.first << ' ' << nxt.second << endl;
//			cerr << f + max(2 * (arr[i][0] - be.first - 1), 0) << endl;
			if(be.first != arr[i][0] && (be.second == 5 || adv(be, arr[i][1] - 1 - j, 2) < mpr(arr[i][0], f) ||
			arr[i][1] - 1 - j <= f + 2 * (arr[i][0] - be.first - 1)))
				be.first++, be.second = 0;
			orig[j] = be.first;
		}
		f = nxt;
//		cerr << endl;
	}
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	printf("%d\n", orig[n - 1]);
	for(int i = 0; i < n; i++) printf("%d ", orig[i]);
	printf("\n");

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lowb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs(a) < eps && abs(b) < eps) return 0;
	else if(abs1(a - b) / abs1(a) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename t>
inline int jud(t a, t b){
	if(a < b) return -1;
	if(a == b) return 0;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1) be--;
		if(!f_small && jud(*(a + be), val) == -1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1) be--;
		if(f_small && jud(*(a + be), val) == 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

const int maxn = 200010;
int dp[maxn][7];
int orig[maxn], arr[maxn][2], larr;
int stat[maxn];
int n;

inline pii adv(pii now, int add, int base = 5) {
	pii ans = now;
	if(ans.second >= base) {
		ans.first++;
		ans.second = 0;
		add--;
	}
	ans.second += add;
	ans.first += ans.second / base;
	ans.second %= base;
	return ans;
}
inline bool judge(pii a, pii b, int l) {
	int small = (b.first - a.first - 1) * 2 + b.second + 1 + int(a.second == 0);
	int big = (b.first - a.first - 1) * 5 + b.second + 5 - a.second;
	return l >= small && l <= big;
}

int main() {
	ios_base::sync_with_stdio(0);
//	freopen("input.txt", "r", stdin);
	scanf("%d", &n);
	if(n == 1) {
		printf("-1\n");
		return 0;
	}
	bool fla = 1;
	for(int i = 0; i < n; i++) {
		scanf("%d", orig + i);
		stat[orig[i]]++;
		if(orig[i] != 0 && stat[orig[i]] > 5) fla = 0;
	}
	if(!fla) {
		printf("-1\n");
		return 0;
	}
//	if(n > 100) {
//		for(int i = 0; i < n; i++) if(orig[i] == 0) {
//			printf("%d\n", i - 1);
//			printf("%d ", orig[i - 1]);
//			int j = i;
//			for(; orig[j] == 0 && j != n; j++);
//			printf("%d %d\n", j, orig[j]);
//			i = j;
//		}
//	}

	arr[larr][0] = 0;
	arr[larr][1] = 0;
	larr++;
	for(int i = 0; i < n; i++) if(orig[i] != 0) {
		arr[larr][0] = orig[i];
		arr[larr++][1] = i + 1;
	}
	memset(dp, -1, sizeof(dp));
	dp[0][4] = 0;
	for(int i = 0; i < larr - 1; i++) {
//		if(arr[i][0] == 701)
//			cout << i << endl;
		for(int j = 0; j < 5; j++) if(dp[arr[i][1]][j] != -1) {
//			cerr << 1 << endl;
			int l = arr[i + 1][1] - arr[i][1];
//			pii big = adv(mpr(arr[i][0], j), l, 2);
//			pii small = adv(mpr(arr[i][0], j), l, 5);
//			if(arr[i][0] == 701) {
//				cout << "big: " << big.first << ' ' << big.second << endl;
//				cout << "small: " << small.first << ' ' << small.second << endl;
//			}
			if(arr[i + 1][0] == arr[i][0]) {
				if(j + arr[i + 1][1] - arr[i][1] < 5)
					dp[arr[i + 1][1]][j + arr[i + 1][1] - arr[i][1]] = j;
			} else for(int k = 0; k < 5; k++) {
				if(judge(mpr(arr[i][0], j), mpr(arr[i + 1][0], k), l))
					dp[arr[i + 1][1]][k] = j;
			}
		}
	}
//	for(int i = 0; i < larr; i++) if(arr[i][0] == 701){
//		cout << arr[i][0] << ' ' << arr[i][1] << endl;
//		for(int j = 0; j < 5; j++) {
//			cout << dp[arr[i][1]][j] << ' ';
//		}
//		cout << endl;
//	}

	int f = -1;
	for(int j = 4; j >= 0; j--) if(dp[arr[larr - 1][1]][j] != -1) {
		if(j == 0 && arr[larr - 1][1] == n)
			break;
		f = j;
		break;
	}
	if(f == -1) {
		printf("-1\n");
		return 0;
	}
	int be = arr[larr - 1][1];
	if(f == 0) {
		orig[be] = arr[larr - 1][0];
		be++;
	}
//	cerr << f << endl;
	int flag = 0;
	for(int i = be, add = arr[larr - 1][0] + 1; i < n; i++, flag ^= 1) {
		orig[i] = add;
		if(flag) add++;
	}
	if(flag)
		orig[n - 1] = orig[n - 2];
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	for(int i = larr - 1; i; i--) {
//		cerr << "f: " << f << endl;
//		if(arr[i][0] == 1925) cout << f << ' ' << i << endl;
		int nxt = dp[arr[i][1]][f];
		pii be = mpr(arr[i - 1][0], nxt);
		for(int j = arr[i - 1][1]; j < arr[i][1] - 1; j++) {
			be.second++;
//			pii nxt = adv(be, arr[i ][1] - 1 - j, 2);
//			cerr << arr[i][1] - 1 - j << endl;
//			cerr << "be: " << be.first << ' ' << be.second << endl;
//			cerr << "nxt: " << nxt.first << ' ' << nxt.second << endl;
//			cerr << f + max(2 * (arr[i][0] - be.first - 1), 0) << endl;
			if(be.first != arr[i][0] && (be.second == 5 || adv(be, arr[i][1] - 1 - j, 2) < mpr(arr[i][0], f) ||
			arr[i][1] - 1 - j <= f + 2 * (arr[i][0] - be.first - 1)))
				be.first++, be.second = 0;
			orig[j] = be.first;
		}
		f = nxt;
//		cerr << endl;
	}
//	for(int i = 0; i < n; i++)
//		cerr << orig[i] << ' ';
//	cerr << endl;
	printf("%d\n", orig[n - 1]);
	for(int i = 0; i < n; i++) printf("%d ", orig[i]);
	printf("\n");

	return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

int main() {
  int n;
  vector<int> p;
  p.push_back(0);

  cin >> n;
  int a[n+1], status[6][n+1];
  memset(status, 0, sizeof(int)*6*(n+1));
  a[0] = 0;
  for(int i = 0; i < n; i ++) {
    cin >> a[i+1];
    if(a[i+1]) p.push_back(i+1);
  }
  if(p.size() == 1) {
    cout << n/2 << endl;
    for(int i = 0; i < n/2; i ++) cout << i+1 << " " << i+1 << " ";
    if(n%2) cout << n/2 << endl;
    else cout << endl;
    return 0;
  } else if(a[1] > 1 || a[2] > 1) {
    cout << -1 << endl;
    return 0;
  }
  status[5][0] = 1;
  for(int i = 1; i < p.size(); i ++) {
    if(a[p[i]] < a[p[i-1]]) {
      cout << -1 << endl;
      return 0;
    } else if(a[p[i]] == a[p[i-1]]) {
      for(int j = p[i-1]+1; j < p[i]; j ++) a[j] = a[p[i]];
      int dis = p[i] - p[i-1];
      for(int y = 1; y <= 5; y ++) {
        if(y + dis > 5) continue;
        else if(status[y][p[i-1]]) status[y+dis][p[i]] |= 1<<y;
      }
    } else {
      for(int x = 0; x < 5; x ++) {
        for(int y = 1; y <= 5; y ++) {
          int l = p[i] - p[i-1] - x - y;
          if(l < 2*(a[p[i]] - a[p[i-1]] - 1) || l > 5*(a[p[i]] - a[p[i-1]] - 1)) continue;
          for(int j = 1; j <= 5; j ++) {
            if(x + j < 2 || x + j > 5) continue;
            else if(status[j][p[i-1]]) status[y][p[i]] |= 1 << (x*6+j);
          }
        }
      }
    }
  }

  int ans = 0, y, l;
  for(int x = 1; x <= 5; x ++) {
    if(status[x][p.back()] == 0) continue;
    y = n - p.back();
    if(x == 1 && y < 1) continue;
    else if(x == 5 && y == 1) continue;
    if(x == 1) y-= 1;
    y = a[p.back()] + y/2;
    if(y > ans) ans = y, l = x;
  }
  if(ans == 0) {
    cout << -1 << endl;
    return 0;
  }
  int x = p.back() + 1;
  if(l == 1) a[x] = a[x-1], x ++;
  for(int i = x; i < n/2*2; i +=2) a[i] = a[i+1] = a[i-1] + 1;
  a[n] = a[n-1];

  for(int i = p.size() - 1; i > 0; i --) {
    if(a[p[i]] == a[p[i-1]]) {
      l -= p[i] - p[i-1];
      continue;
    } else {
      for(int j = p[i] - l + 1; j < p[i]; j ++) a[j] = a[p[i]];
      for(int s = 1; s < 30; s ++) {
        if(status[l][p[i]] & 1 << s) {
          int x = s / 6, dist = p[i] - l - p[i-1] - x + 1;
          for(int j = p[i-1] + 1; j <= p[i-1] + x; j ++) a[j] = a[p[i-1]];
          for(int j = p[i-1] + x + 1; j < p[i] - l + 1; j ++)
            a[j] = a[p[i-1]] + 1 + (j - p[i-1] - x) * (a[p[i]] - a[p[i-1]] - 1) / dist;
          l = s%6;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  for(int i = 1; i <= n; i ++)
    cout << a[i] << " ";
  cout << endl;
  return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

int main() {
  int n;
  vector<int> p;
  p.push_back(0);

  cin >> n;
  int a[n+1], status[6][n+1];
  memset(status, 0, sizeof(int)*6*(n+1));
  a[0] = 0;
  for(int i = 0; i < n; i ++) {
    cin >> a[i+1];
    if(a[i+1]) p.push_back(i+1);
  }
  if(p.size() == 1) {
    cout << n/2 << endl;
    for(int i = 0; i < n/2; i ++) cout << i+1 << " " << i+1 << " ";
    if(n%2) cout << n/2 << endl;
    else cout << endl;
    return 0;
  } else if(a[1] > 1 || a[2] > 1) {
    cout << -1 << endl;
    return 0;
  }
  status[5][0] = 1;
  for(int i = 1; i < p.size(); i ++) {
    if(a[p[i]] < a[p[i-1]]) {
      cout << -1 << endl;
      return 0;
    } else if(a[p[i]] == a[p[i-1]]) {
      for(int j = p[i-1]+1; j < p[i]; j ++) a[j] = a[p[i]];
      int dis = p[i] - p[i-1];
      for(int y = 1; y <= 5; y ++) {
        if(y + dis > 5) continue;
        else if(status[y][p[i-1]]) status[y+dis][p[i]] |= 1<<y;
      }
    } else {
      for(int x = 0; x < 5; x ++) {
        for(int y = 1; y <= 5; y ++) {
          int l = p[i] - p[i-1] - x - y;
          if(l < 2*(a[p[i]] - a[p[i-1]] - 1) || l > 5*(a[p[i]] - a[p[i-1]] - 1)) continue;
          for(int j = 1; j <= 5; j ++) {
            if(x + j < 2 || x + j > 5) continue;
            else if(status[j][p[i-1]]) status[y][p[i]] |= 1 << (x*6+j);
          }
        }
      }
    }
  }

  int ans = 0, y, l;
  for(int x = 1; x <= 5; x ++) {
    if(status[x][p.back()] == 0) continue;
    y = n - p.back();
    if(x == 1 && y < 1) continue;
    else if(x == 5 && y == 1) continue;
    if(x == 1) y-= 1;
    y = a[p.back()] + y/2;
    if(y > ans) ans = y, l = x;
  }
  if(ans == 0) {
    cout << -1 << endl;
    return 0;
  }
  int x = p.back() + 1;
  if(l == 1) a[x] = a[x-1], x ++;
  for(int i = x; i <= n/2*2; i +=2) a[i] = a[i+1] = a[i-1] + 1;
  a[n] = a[n-1];

  for(int i = p.size() - 1; i > 0; i --) {
    if(a[p[i]] == a[p[i-1]]) {
      l -= p[i] - p[i-1];
      continue;
    } else {
      for(int j = p[i] - l + 1; j < p[i]; j ++) a[j] = a[p[i]];
      for(int s = 1; s < 30; s ++) {
        if(status[l][p[i]] & 1 << s) {
          int x = s / 6, dist = p[i] - l - p[i-1] - x + 1;
          for(int j = p[i-1] + 1; j <= p[i-1] + x; j ++) a[j] = a[p[i-1]];
          for(int j = p[i-1] + x + 1; j < p[i] - l + 1; j ++)
            a[j] = a[p[i-1]] + 1 + (j - p[i-1] - x) * (a[p[i]] - a[p[i-1]] - 1) / dist;
          l = s%6;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  for(int i = 1; i <= n; i ++)
    cout << a[i] << " ";
  cout << endl;
  return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

int main() {
  int n;
  vector<int> p;
  p.push_back(0);

  cin >> n;
  int a[n+1], status[6][n+1];
  memset(status, 0, sizeof(int)*6*(n+1));
  a[0] = 0;
  for(int i = 0; i < n; i ++) {
    cin >> a[i+1];
    if(a[i+1]) p.push_back(i+1);
  }
  if(p.size() == 1) {
    cout << n/2 << endl;
    for(int i = 0; i < n/2; i ++) cout << i+1 << " " << i+1 << " ";
    if(n%2) cout << n/2 << endl;
    else cout << endl;
    return 0;
  } else if(a[1] > 1 || a[2] > 1) {
    cout << -1 << endl;
    return 0;
  }
  status[5][0] = 1;
  for(int i = 1; i < p.size(); i ++) {
    if(a[p[i]] < a[p[i-1]]) {
      cout << -1 << endl;
      return 0;
    } else if(a[p[i]] == a[p[i-1]]) {
      for(int j = p[i-1]+1; j < p[i]; j ++) a[j] = a[p[i]];
      int dis = p[i] - p[i-1];
      for(int y = 1; y <= 5; y ++) {
        if(y + dis > 5) continue;
        else if(status[y][p[i-1]]) status[y+dis][p[i]] |= 1<<y;
      }
    } else {
      for(int x = 0; x < 5; x ++) {
        for(int y = 1; y <= 5; y ++) {
          int l = p[i] - p[i-1] - x - y;
          if(l < 2*(a[p[i]] - a[p[i-1]] - 1) || l > 5*(a[p[i]] - a[p[i-1]] - 1)) continue;
          for(int j = 1; j <= 5; j ++) {
            if(x + j < 2 || x + j > 5) continue;
            else if(status[j][p[i-1]]) status[y][p[i]] |= 1 << (x*6+j);
          }
        }
      }
    }
  }

  int ans = 0, y, l;
  for(int x = 1; x <= 5; x ++) {
    if(status[x][p.back()] == 0) continue;
    y = n - p.back();
    if(x == 1 && y < 1) continue;
    else if(x == 5 && y == 1) continue;
    if(x == 1) y-= 1;
    y = a[p.back()] + y/2;
    if(y > ans) ans = y, l = x;
  }
  if(ans == 0) {
    cout << -1 << endl;
    return 0;
  }
  int x = p.back() + 1;
  if(l == 1) a[x] = a[x-1], x ++;
  while(x < n) a[x] = a[x+1] = a[x-1]+1, x+=2;
  if(x == n) a[x] = a[x-1] , x++;

  for(int i = p.size() - 1; i > 0; i --) {
    if(a[p[i]] == a[p[i-1]]) {
      l -= p[i] - p[i-1];
      continue;
    } else {
      for(int j = p[i] - l + 1; j < p[i]; j ++) a[j] = a[p[i]];
      for(int s = 1; s < 30; s ++) {
        if(status[l][p[i]] & 1 << s) {
          int x = s / 6, dist = p[i] - l - p[i-1] - x + 1;
          for(int j = p[i-1] + 1; j <= p[i-1] + x; j ++) a[j] = a[p[i-1]];
          for(int j = p[i-1] + x + 1; j < p[i] - l + 1; j ++)
            a[j] = a[p[i-1]] + 1 + (j - p[i-1] - x) * (a[p[i]] - a[p[i-1]] - 1) / dist;
          l = s%6;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  for(int i = 1; i <= n; i ++)
    cout << a[i] << " ";
  cout << endl;
  return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

int main() {
  int n;
  vector<int> p;
  p.push_back(0);

  cin >> n;
  int a[n+1], status[6][n+1];
  memset(status, 0, sizeof(int)*6*(n+1));
  a[0] = 0;
  for(int i = 0; i < n; i ++) {
    cin >> a[i+1];
    if(a[i+1]) p.push_back(i+1);
  }
  if(p.size() == 1) {
    cout << n/2 << endl;
    for(int i = 0; i < n/2; i ++) cout << i+1 << " " << i+1 << " ";
    if(n%2) cout << n/2 << endl;
    else cout << endl;
    return 0;
  } else if(a[1] > 1 || a[2] > 1) {
    cout << -1 << endl;
    return 0;
  }
  status[5][0] = 1;
  for(int i = 1; i < p.size(); i ++) {
    if(a[p[i]] < a[p[i-1]]) {
      cout << -1 << endl;
      return 0;
    } else if(a[p[i]] == a[p[i-1]]) {
      for(int j = p[i-1]+1; j < p[i]; j ++) a[j] = a[p[i]];
      int dis = p[i] - p[i-1];
      for(int y = 1; y <= 5; y ++) {
        if(y + dis > 5) continue;
        else if(status[y][p[i-1]]) status[y+dis][p[i]] |= 1<<y;
      }
    } else {
      for(int x = 0; x < 5; x ++) {
        for(int y = 1; y <= 5; y ++) {
          int l = p[i] - p[i-1] - x - y;
          if(l < 2*(a[p[i]] - a[p[i-1]] - 1) || l > 5*(a[p[i]] - a[p[i-1]] - 1)) continue;
          for(int j = 1; j <= 5; j ++) {
            if(x + j < 2 || x + j > 5) continue;
            else if(status[j][p[i-1]]) status[y][p[i]] |= 1 << (x*6+j);
          }
        }
      }
    }
  }

  int ans = 0, y, l;
  for(int x = 1; x <= 5; x ++) {
    if(status[x][p.back()] == 0) continue;
    y = n - p.back();
    if(x == 1 && y < 1) continue;
    else if(x == 5 && y == 1) continue;
    if(x == 1) y-= 1;
    y = a[p.back()] + y/2;
    if(y > ans) ans = y, l = x;
  }
  if(ans == 0) {
    cout << -1 << endl;
    return 0;
  }
  int x = p.back() + 1;
  if(l == 1) a[x] = a[x-1], x ++;
  for(int i = x; i < n/2*2; i +=2) a[i] = a[i+1] = a[i-1];
  a[n] = a[n-1];

  for(int i = p.size() - 1; i > 0; i --) {
    if(a[p[i]] == a[p[i-1]]) {
      l -= p[i] - p[i-1];
      continue;
    } else {
      for(int j = p[i] - l + 1; j < p[i]; j ++) a[j] = a[p[i]];
      for(int s = 1; s < 30; s ++) {
        if(status[l][p[i]] & 1 << s) {
          int x = s / 6, dist = p[i] - l - p[i-1] - x + 1;
          for(int j = p[i-1] + 1; j <= p[i-1] + x; j ++) a[j] = a[p[i-1]];
          for(int j = p[i-1] + x + 1; j < p[i] - l + 1; j ++)
            a[j] = a[p[i-1]] + 1 + (j - p[i-1] - x) * (a[p[i]] - a[p[i-1]] - 1) / dist;
          l = s%6;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  for(int i = 1; i <= n; i ++)
    cout << a[i] << " ";
  cout << endl;
  return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

int main() {
  int n;
  vector<int> p;
  p.push_back(0);

  cin >> n;
  int a[n+1], status[6][n+1];
  memset(status, 0, sizeof(int)*6*(n+1));
  a[0] = 0;
  for(int i = 0; i < n; i ++) {
    cin >> a[i+1];
    if(a[i+1]) p.push_back(i+1);
  }
  if(p.size() == 1) {
    cout << n/2 << endl;
    for(int i = 0; i < n/2; i ++) cout << i+1 << " " << i+1 << " ";
    if(n%2) cout << n/2 << endl;
    else cout << endl;
    return 0;
  } else if(a[1] > 1 || a[2] > 1) {
    cout << -1 << endl;
    return 0;
  }
  status[5][0] = 1;
  for(int i = 1; i < p.size(); i ++) {
    if(a[p[i]] < a[p[i-1]]) {
      cout << -1 << endl;
      return 0;
    } else if(a[p[i]] == a[p[i-1]]) {
      for(int j = p[i-1]+1; j < p[i]; j ++) a[j] = a[j-1];
      int dis = p[i] - p[i-1];
      for(int y = 1; y <= 5; y ++) {
        if(y + dis < 2 || y + dis > 5) continue;
        else if(status[y][p[i-1]]) status[y+dis][p[i]] |= 1<<y;
      }
    } else {
      for(int x = 0; x < 5; x ++) {
        for(int y = 1; y <= 5; y ++) {
          int l = p[i] - p[i-1] - x - y;
          if(l < 2*(a[p[i]] - a[p[i-1]] - 1) || l > 5*(a[p[i]] - a[p[i-1]] - 1)) continue;
          for(int j = 1; j <= 5; j ++) {
            if(x + j < 2 || x + j > 5) continue;
            else if(status[j][p[i-1]]) status[y][p[i]] |= 1 << (x*6+j);
          }
        }
      }
    }
  }

  int ans = 0, y, l;
  for(int x = 1; x <= 5; x ++) {
    if(status[x][p.back()] == 0) continue;
    y = n - p.back();
    if(x == 1 && y < 1) continue;
    else if(x == 5 && y == 1) continue;
    if(x == 1) y-= 1;
    y = a[p.back()] + y/2;
    if(y > ans) ans = y, l = x;
  }
  if(ans == 0) {
    cout << -1 << endl;
    return 0;
  }
  int x = p.back() + 1;
  if(l == 1) a[x] = a[x-1], x ++;
  for(int i = x; i < n/2*2; i +=2) a[i] = a[i+1] = a[i-1];
  a[n] = a[n-1];

  for(int i = p.size() - 1; i > 0; i --) {
    if(a[p[i]] == a[p[i-1]]) {
      l -= p[i] - p[i-1];
      continue;
    } else {
      for(int j = p[i] - l + 1; j < p[i]; j ++) a[j] = a[p[i]];
      for(int s = 1; s < 30; s ++) {
        if(status[l][p[i]] & 1 << s) {
          int x = s / 6, dist = p[i] - l - p[i-1] - x;
          for(int j = p[i-1] + 1; j <= p[i-1] + x; j ++) a[j] = a[p[i-1]];
          for(int j = p[i-1] + x + 1; j < p[i] - l + 1; j ++)
            a[j] = a[p[i-1]] + 1 + (j - p[i-1] - x) * (a[p[i]] - a[p[i-1]] - 1) / dist;
          l = x%6;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  for(int i = 1; i <= n; i ++)
    cout << a[i] << " ";
  cout << endl;
  return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main() {
  int n;
  vector<int> p;
  p.push_back(0);

  cin >> n;
  int a[n+1], status[6][n+1];
  a[0] = 0;
  for(int i = 0; i < n; i ++) {
    cin >> a[i+1];
    if(a[i+1]) p.push_back(i+1);
  }
  if(p.size() == 1) {
    cout << n/2 << endl;
    for(int i = 0; i < n/2; i ++) cout << i+1 << " " << i+1 << " ";
    if(n%2) cout << n/2 << endl;
    else cout << endl;
    return 0;
  } else if(a[1] > 1 || a[2] > 1) {
    cout << -1 << endl;
    return 0;
  }
  status[5][0] = 1;
  for(int i = 1; i < p.size(); i ++) {
    if(a[p[i]] < a[p[i-1]]) {
      cout << -1 << endl;
      return 0;
    } else if(a[p[i]] == a[p[i-1]]) {
      for(int j = p[i-1]+1; j < p[i]; j ++) a[j] = a[j-1];
      int dis = p[i] - p[i-1];
      for(int y = 1; y <= 5; y ++) {
        if(y + dis < 2 || y + dis > 5) continue;
        else if(status[y][p[i-1]]) status[y+dis][p[i]] |= 1<<y;
      }
    } else {
      for(int x = 0; x < 5; x ++) {
        for(int y = 1; y <= 5; y ++) {
          int l = p[i] - p[i-1] - x - y;
          if(l < 2*(a[p[i]] - a[p[i-1]] - 1) || l > 5*(a[p[i]] - a[p[i-1]] - 1)) continue;
          for(int j = 1; j <= 5; j ++) {
            if(x + j < 2 || x + j > 5) continue;
            else if(status[j][p[i-1]]) status[y][p[i]] |= 1 << (x*6+j);
          }
        }
      }
    }
  }

  int ans = 0, y, l;
  for(int x = 1; x <= 5; x ++) {
    if(status[x][p.back()] == 0) continue;
    y = n - p.back();
    if(x == 1 && y < 1) continue;
    else if(x == 5 && y == 1) continue;
    if(x == 1) y-= 1;
    y = a[p.back()] + y/2;
    if(y > ans) ans = y, l = x;
  }
  if(ans == 0) {
    cout << -1 << endl;
    return 0;
  }
  int x = p.back() + 1;
  if(l == 1) a[x] = a[x-1], x ++;
  for(int i = x; i < n/2*2; i +=2) a[i] = a[i+1] = a[i-1];
  a[n] = a[n-1];

  for(int i = p.size() - 1; i > 0; i --) {
    if(a[p[i]] == a[p[i-1]]) {
      l -= p[i] - p[i-1];
      continue;
    } else {
      for(int j = p[i] - l + 1; j < p[i]; j ++) a[j] = a[p[i]];
      for(int s = 1; s < 30; s ++) {
        if(status[l][p[i]] & 1 << s) {
          int x = s / 6, dist = p[i] - l - p[i-1] - x;
          for(int j = p[i-1] + 1; j <= p[i-1] + x; j ++) a[j] = a[p[i-1]];
          for(int j = p[i-1] + x + 1; j < p[i] - l + 1; j ++)
            a[j] = a[p[i-1]] + 1 + (j - p[i-1] - x) * (a[p[i]] - a[p[i-1]] - 1) / dist;
          l = x%6;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  for(int i = 1; i <= n; i ++)
    cout << a[i] << " ";
  cout << endl;
  return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 200010
using namespace std;
bool ok[maxn][7];
int n,v[maxn],pre[maxn];
void read()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%d",v+i);
}
void print(int n,int m)
{
    if(v[1]>1)
    {
        printf("-1\n");
        return ;
    }
    v[1]=1;
    if(n==1)
    {
        printf("1 ");
        return ;
    }
    int p=pre[n];
    if(v[p]==v[n])
    {
        for(int a=1;a+n-p<=5;++a)
            if(ok[p][a])
            {
                print(p,a);
                for(int j=p+1;j<=n;++j)
                    printf("%d ",v[n]);
                return ;
            }
    }
    else
    {
        for(int a=1;a<=5;++a)
            if(ok[p][a])
                for(int b=max(0,2-a);b<=5-a;++b)
                {
                    int t=n-b-m-p;
                    if(t<0||(v[n]-v[p]-1)*2>t||(v[n]-v[p]-1)*5<t)
                        continue;
                    print(p,a);
                    for(int i=1;i<=b;++i)
                        printf("%d ",v[p]);
                    for(int i=v[p]+1;i<v[n];++i)
                    {
                        for(int j=1;j<=5;++j)
                        {
                            --t;
                            printf("%d ",i);
                            if(j>=2&&(v[n]-i-1)*2<=t&&(v[n]-i-1)*5>=t)
                                break;
                        }
                    }
                    for(int i=1;i<=m;++i)
                        printf("%d ",v[n]);
                    return ;
                }
    }
}
void print()
{
    ok[1][1]=true;
    int last=1;
    for(int i=2;i<=n;++i)
        if(v[i])
        {
            if(v[i]==v[last])
            {
                for(int a=1;a+i-last<=5;++a)
                    if(ok[last][a])
                        ok[i][a+i-last]=true;
            }
            else
            {
                for(int c=1;c<=5;++c)
                    for(int a=1;a<=5&&!ok[i][c];++a)
                        if(ok[last][a])
                            for(int b=max(0,2-a);b<=5-a;++b)
                            {
                                int t=i-last-b-c;
                                if(t<0)
                                    continue;
                                if((v[i]-v[last]-1)*2<=t&&(v[i]-v[last]-1)*5>=t)
                                    ok[i][c]=true;
                            }
            }
            pre[i]=last,last=i;
            if(count(ok[i]+1,ok[i]+6,true)==0)
            {
                printf("-1\n");
                return ;
            }
        }
    if(last==n&&count(ok[n]+2,ok[n]+6,true)==0)
    {
        printf("-1\n");
        return ;
    }
    for(int i=5;i>=1;--i)
        if(ok[last][i])
        {
            int j=last,cnt;
            if(i==1)
                ++j,v[j]=v[j-1];
            for(j=j+1,cnt=1;j<=n;++j)
            {
                if(j==n)
                    v[j]=v[j-1],++cnt;
                else
                {
                    if(cnt==1)
                        v[j]=v[j-1]+1,++cnt;
                    else
                        v[j]=v[j-1],cnt=1;
                }
            }
            printf("%d\n",v[n]);
            print(last,i);
            for(j=last+1;j<=n;++j)
                printf("%d ",v[j]);
            return ;
        }
}
int main()
{
    read();
    print();
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 200010
using namespace std;
bool ok[maxn][7];
int n,v[maxn],pre[maxn];
void read()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%d",v+i);
}
void print(int n,int m)
{
    if(n==1)
    {
        printf("1 ");
        return ;
    }
    int p=pre[n];
    if(v[p]==v[n])
    {
        for(int a=1;a+n-p<=5;++a)
            if(ok[p][a])
            {
                print(p,a);
                for(int j=p+1;j<=n;++j)
                    printf("%d ",v[n]);
                return ;
            }
    }
    else
    {
        for(int a=1;a<=5;++a)
            if(ok[p][a])
                for(int b=max(0,2-a);b<=5-a;++b)
                {
                    int t=n-b-m-p;
                    if(t<0||(v[n]-v[p]-1)*2>t||(v[n]-v[p]-1)*5<t)
                        continue;
                    print(p,a);
                    for(int i=1;i<=b;++i)
                        printf("%d ",v[p]);
                    for(int i=v[p]+1;i<v[n];++i)
                    {
                        for(int j=1;j<=5;++j)
                        {
                            --t;
                            printf("%d ",i);
                            if(j>=2&&(v[n]-i-1)*2<=t&&(v[n]-i-1)*5>=t)
                                break;
                        }
                    }
                    for(int i=1;i<=m;++i)
                        printf("%d ",v[n]);
                    return ;
                }
    }
}
void print()
{
    if(v[1]>1)
    {
        printf("-1\n");
        return ;
    }
    v[1]=1;
    ok[1][1]=true;
    int last=1;
    for(int i=2;i<=n;++i)
        if(v[i])
        {
            if(v[i]==v[last])
            {
                for(int a=1;a+i-last<=5;++a)
                    if(ok[last][a])
                        ok[i][a+i-last]=true;
            }
            else
            {
                for(int c=1;c<=5;++c)
                    for(int a=1;a<=5&&!ok[i][c];++a)
                        if(ok[last][a])
                            for(int b=max(0,2-a);b<=5-a;++b)
                            {
                                int t=i-last-b-c;
                                if(t<0)
                                    continue;
                                if((v[i]-v[last]-1)*2<=t&&(v[i]-v[last]-1)*5>=t)
                                    ok[i][c]=true;
                            }
            }
            pre[i]=last,last=i;
            if(count(ok[i]+1,ok[i]+6,true)==0)
            {
                printf("-1\n");
                return ;
            }
        }
    if(last==n&&count(ok[n]+2,ok[n]+6,true)==0)
    {
        printf("-1\n");
        return ;
    }
    for(int i=5;i>=1;--i)
        if(ok[last][i])
        {
            int j=last,cnt;
            if(i==1)
                ++j,v[j]=v[j-1];
            for(j=j+1,cnt=1;j<=n;++j)
            {
                if(j==n)
                    v[j]=v[j-1],++cnt;
                else
                {
                    if(cnt==1)
                        v[j]=v[j-1]+1,++cnt;
                    else
                        v[j]=v[j-1],cnt=1;
                }
            }
            printf("%d\n",v[n]);
            print(last,i);
            for(j=last+1;j<=n;++j)
                printf("%d ",v[j]);
            return ;
        }
}
int main()
{
    read();
    print();
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 200010
using namespace std;
bool ok[maxn][7];
int n,v[maxn],pre[maxn];
void read()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%d",v+i);
    v[1]=1;
}
void print(int n,int m)
{
    if(n==1)
    {
        printf("1 ");
        return ;
    }
    int p=pre[n];
    if(v[p]==v[n])
    {
        for(int a=1;a+n-p<=5;++a)
            if(ok[p][a])
            {
                print(p,a);
                for(int j=p+1;j<=n;++j)
                    printf("%d ",v[n]);
                return ;
            }
    }
    else
    {
        for(int a=1;a<=5;++a)
            if(ok[p][a])
                for(int b=max(0,2-a);b<=5-a;++b)
                {
                    int t=n-a-b-m;
                    if(t<0||(v[n]-v[p]-1)*2>t||(v[n]-v[p]-1)*5<t)
                        continue;
                    print(p,a);
                    for(int i=1;i<=b;++i)
                        printf("%d ",v[p]);
                    for(int i=v[p]+1;i<v[n];++i)
                    {
                        for(int j=1;j<=5;++j)
                        {
                            --t;
                            printf("%d ",i);
                            if(j>=2&&(v[n]-i-1)*2<=t&&(v[n]-i-1)*5>=t)
                                break;
                        }
                    }
                    for(int i=1;i<=m;++i)
                        printf("%d ",v[n]);
                    return ;
                }
    }
}
void print()
{
    ok[1][1]=true;
    int last=1;
    for(int i=2;i<=n;++i)
        if(v[i])
        {
            if(v[i]==v[last])
            {
                for(int a=1;a+i-last<=5;++a)
                    if(ok[last][a])
                        ok[i][a+i-last]=true;
            }
            else
            {
                for(int c=1;c<=5;++c)
                    for(int a=1;a<=5&&!ok[i][c];++a)
                        if(ok[last][a])
                            for(int b=max(0,2-a);b<=5-a;++b)
                            {
                                int t=i-last-b-c;
                                if(t<0)
                                    continue;
                                if((v[i]-v[last]-1)*2<=t&&(v[i]-v[last]-1)*5>=t)
                                    ok[i][c]=true;
                            }
            }
            pre[i]=last,last=i;
            if(count(ok[i]+1,ok[i]+6,true)==0)
            {
                printf("-1\n");
                return ;
            }
        }
    if(last==n&&count(ok[n]+2,ok[n]+6,true)==0)
    {
        printf("-1\n");
        return ;
    }
    for(int i=5;i>=1;--i)
        if(ok[last][i]&&last!=n)
        {
            print(last,i);
            int j=last,cnt;
            if(i==1)
            {
                ++j,v[j]=v[j-1];
                printf("%d ",v[j]);
            }
            for(j=j+1,cnt=1;j<=n;++j)
            {
                if(j==n)
                    v[j]=v[j-1];
                else
                {
                    if(cnt==1)
                        v[j]=v[j-1]+1,++cnt;
                    else
                        v[j]=v[j-1],cnt=1;
                }
                printf("%d ",v[j]);
            }
            return ;
        }
}
int main()
{
    read();
    print();
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 200010
using namespace std;
bool ok[maxn][7];
int n,v[maxn],pre[maxn];
void read()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%d",v+i);
    v[1]=1;
}
void print(int n,int m)
{
    if(n==1)
    {
        printf("1 ");
        return ;
    }
    int p=pre[n];
    if(v[p]==v[n])
    {
        for(int a=1;a+n-p<=5;++a)
            if(ok[p][a])
            {
                print(p,a);
                for(int j=p+1;j<=n;++j)
                    printf("%d ",v[n]);
                return ;
            }
    }
    else
    {
        for(int a=1;a<=5;++a)
            if(ok[p][a])
                for(int b=max(0,2-a);b<=5-a;++b)
                {
                    int t=n-a-b-m;
                    if(t<0||(v[n]-v[p]-1)*2>t||(v[n]-v[p]-1)*5<t)
                        continue;
                    print(p,a);
                    for(int i=1;i<=b;++i)
                        printf("%d ",v[p]);
                    for(int i=v[p]+1;i<v[n];++i)
                    {
                        for(int j=1;j<=5;++j)
                        {
                            --t;
                            printf("%d ",i);
                            if(j>=2&&(v[n]-i-1)*2<=t&&(v[n]-i-1)*5>=t)
                                break;
                        }
                    }
                    for(int i=1;i<=m;++i)
                        printf("%d ",v[n]);
                    return ;
                }
    }
}
void print()
{
    ok[1][1]=true;
    int last=1;
    for(int i=2;i<=n;++i)
        if(v[i])
        {
            if(v[i]==v[last])
            {
                for(int a=1;a+i-last<=5;++a)
                    if(ok[last][a])
                        ok[i][a+i-last]=true;
            }
            else
            {
                for(int c=1;c<=5;++c)
                    for(int a=1;a<=5&&!ok[i][c];++a)
                        if(ok[last][a])
                            for(int b=max(0,2-a);b<=5-a;++b)
                            {
                                int t=i-last-b-c;
                                if(t<0)
                                    continue;
                                if((v[i]-v[last]-1)*2<=t&&(v[i]-v[last]-1)*5>=t)
                                    ok[i][c]=true;
                            }
            }
            pre[i]=last,last=i;
            if(count(ok[i]+1,ok[i]+6,true)==0)
            {
                printf("-1\n");
                return ;
            }
        }
    if(last==n&&count(ok[n]+2,ok[n]+6,true)==0)
    {
        printf("-1\n");
        return ;
    }
    for(int i=5;i>=1;--i)
        if(ok[last][i]&&last!=n)
        {
            int j=last,cnt;
            if(i==1)
                ++j,v[j]=v[j-1];
            for(j=j+1,cnt=1;j<=n;++j)
            {
                if(j==n)
                    v[j]=v[j-1],++cnt;
                else
                {
                    if(cnt==1)
                        v[j]=v[j-1]+1,++cnt;
                    else
                        v[j]=v[j-1],cnt=1;
                }
            }
            printf("%d\n",v[n]);
            print(last,i);
            for(j=last+1;j<=n;++j)
                printf("%d ",v[j]);
            return ;
        }
}
int main()
{
    read();
    print();
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 200010
using namespace std;
bool ok[maxn][7];
int n,v[maxn],pre[maxn];
void read()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%d",v+i);
    v[1]=1;
}
void print(int n,int m)
{
    if(n==1)
    {
        printf("1 ");
        return ;
    }
    int p=pre[n];
    if(v[p]==v[n])
    {
        for(int a=1;a+n-p<=5;++a)
            if(ok[p][a])
            {
                print(p,a);
                for(int j=p+1;j<=n;++j)
                    printf("%d ",v[n]);
                return ;
            }
    }
    else
    {
        for(int a=1;a<=5;++a)
            if(ok[p][a])
                for(int b=max(0,2-a);b<=5-a;++b)
                {
                    int t=n-b-m-p;
                    if(t<0||(v[n]-v[p]-1)*2>t||(v[n]-v[p]-1)*5<t)
                        continue;
                    print(p,a);
                    for(int i=1;i<=b;++i)
                        printf("%d ",v[p]);
                    for(int i=v[p]+1;i<v[n];++i)
                    {
                        for(int j=1;j<=5;++j)
                        {
                            --t;
                            printf("%d ",i);
                            if(j>=2&&(v[n]-i-1)*2<=t&&(v[n]-i-1)*5>=t)
                                break;
                        }
                    }
                    for(int i=1;i<=m;++i)
                        printf("%d ",v[n]);
                    return ;
                }
    }
}
void print()
{
    ok[1][1]=true;
    int last=1;
    for(int i=2;i<=n;++i)
        if(v[i])
        {
            if(v[i]==v[last])
            {
                for(int a=1;a+i-last<=5;++a)
                    if(ok[last][a])
                        ok[i][a+i-last]=true;
            }
            else
            {
                for(int c=1;c<=5;++c)
                    for(int a=1;a<=5&&!ok[i][c];++a)
                        if(ok[last][a])
                            for(int b=max(0,2-a);b<=5-a;++b)
                            {
                                int t=i-last-b-c;
                                if(t<0)
                                    continue;
                                if((v[i]-v[last]-1)*2<=t&&(v[i]-v[last]-1)*5>=t)
                                    ok[i][c]=true;
                            }
            }
            pre[i]=last,last=i;
            if(count(ok[i]+1,ok[i]+6,true)==0)
            {
                printf("-1\n");
                return ;
            }
        }
    if(last==n&&count(ok[n]+2,ok[n]+6,true)==0)
    {
        printf("-1\n");
        return ;
    }
    for(int i=5;i>=1;--i)
        if(ok[last][i])
        {
            int j=last,cnt;
            if(i==1)
                ++j,v[j]=v[j-1];
            for(j=j+1,cnt=1;j<=n;++j)
            {
                if(j==n)
                    v[j]=v[j-1],++cnt;
                else
                {
                    if(cnt==1)
                        v[j]=v[j-1]+1,++cnt;
                    else
                        v[j]=v[j-1],cnt=1;
                }
            }
            printf("%d\n",v[n]);
            print(last,i);
            for(j=last+1;j<=n;++j)
                printf("%d ",v[j]);
            return ;
        }
}
int main()
{
    read();
    print();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>

using namespace std;

typedef pair<int,int> PII;

#define X first
#define Y second
#define FOR(I,L,R) for(int I=(L);I<(R);I++)
#define FORD(I,R,L) for(int I=(R)-1;I>=(L);I--)
const int MaxN = 200010;

int n,a[MaxN],res[MaxN],cnt[MaxN];
PII op[MaxN],ed[MaxN];

int dp(){
	if(a[0]>1) return 0;
	op[0]=ed[0]=PII(1,1);
	FOR(i,1,n){
		op[i]=PII(op[i-1].X,op[i-1].Y+1);
		ed[i]=PII(ed[i-1].X,ed[i-1].Y+1);
		if(op[i].Y>5) op[i].X++,op[i].Y=1;
		if(ed[i].Y>2) ed[i].X++,ed[i].Y=1;
		if(a[i]){
			if(a[i]<op[i].X || a[i]>ed[i].X) return 0;
			op[i]=max(op[i],PII(a[i],1));
			ed[i]=min(ed[i],PII(a[i],5));
		}
	}
	res[n-1]=ed[n-1].X;
	if(ed[n-1].Y<2) res[n-1]--;
	if(res[n-1]<op[n-1].X) return 0;
	cnt[res[n-1]]++;
	FORD(i,n-1,0){
		res[i]=min(res[i+1],ed[i].X);
		if(cnt[res[i]]>=5) res[i]--;
		cnt[res[i]]++;
	}
	return 1;
}
int main()
{
	scanf("%d",&n);
	FOR(i,0,n) scanf("%d",&a[i]);
	if(!dp()) puts("-1");
	else{
		printf("%d\n",res[n-1]);
		FOR(i,0,n) printf("%d ",res[i]);
		puts("");
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>

using namespace std;

typedef pair<int,int> PII;

#define X first
#define Y second
#define FOR(I,L,R) for(int I=(L);I<(R);I++)
#define FORD(I,R,L) for(int I=(R)-1;I>=(L);I--)
const int MaxN = 200010;

int n,a[MaxN],res[MaxN],cnt[MaxN];
PII op[MaxN],ed[MaxN];

int dp(){
	if(a[0]>1) return 0;
	op[0]=ed[0]=PII(1,1);
	FOR(i,1,n){
		op[i]=op[i-1];ed[i]=ed[i-1];
		if(op[i].Y>5) op[i].X++,op[i].Y=1;
		if(ed[i].Y>2) ed[i].X++,ed[i].Y=1;
		if(a[i]){
			if(a[i]<op[i].X || a[i]>ed[i].X) return 0;
			op[i]=max(op[i],PII(a[i],1));
			ed[i]=min(ed[i],PII(a[i],5));
		}
	}
	res[n-1]=ed[n-1].X;
	if(ed[n-1].Y<2) res[n-1]--;
	if(res[n-1]<op[n-1].X) return 0;
	cnt[res[n-1]]++;
	FORD(i,n-1,0){
		res[i]=min(res[i+1],ed[i].X);
		if(cnt[res[i]]>=5) res[i]--;
		cnt[res[i]]++;
	}
	return 1;
}
int main()
{
	scanf("%d",&n);
	FOR(i,0,n) scanf("%d",&a[i]);
	if(!dp()) puts("-1");
	else{
		printf("%d\n",res[n-1]);
		FOR(i,0,n) printf("%d ",res[n-1]);
		puts("");
	}
	return 0;
}


#include <cstdio>
#include <set>
#include <utility>
using namespace std;
#define MP make_pair
typedef long long int llint;
typedef pair<int,int> PII;
#define MAXN 200033

int n,num[MAXN],nxt[MAXN];
set<PII> vis[MAXN];

bool maybe(int idx,int cnt)
{
	int np,delta,length;
	if(cnt>5)return false;
	np=nxt[idx];
	if(np>n)return true;
	if(num[idx-1]>num[np])return false;
	delta=num[np]-num[idx-1];
	length=np-(idx-1)-1;
	if(length/2 < delta-1)return false;
	if(length/5 > delta)return false;
	return true;
}

int sol(int idx,int cnt)
{
	int i;

	if(maybe(idx,cnt)==false)return 0;
	if(idx>n)
	{
		if(cnt>=2 && cnt<=5)
		{
			printf("%d\n",num[n]);
			for(i=1;i<=n;i++)
			{
				if(i>1)putchar(' ');
				printf("%d",num[i]);
			}
			putchar('\n');
			return 1;
		}
		return 0;
	}
	if( vis[idx].count( PII(num[idx-1],cnt) ) == true )return 0;
	vis[idx].insert( PII(num[idx-1],cnt) );
	if(num[idx]>0)
	{
		if(num[idx]==num[idx-1])return sol(idx+1,cnt+1);
		else if(cnt>=2)return sol(idx+1,1);
	}
	else
	{
		if(cnt>=2)
		{
			num[idx]=num[idx-1]+1;
			if(sol(idx+1,1))return 1;
			num[idx]=0;
		}
		if(cnt<5)
		{
			num[idx]=num[idx-1];
			if(sol(idx+1,cnt+1))return 1;
			num[idx]=0;
		}
	}
	return 0;
}

int main()
{
	int i;

	while(scanf("%d",&n)!=EOF)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
			vis[i].clear();
		}
		if(num[1]>1)
		{
			puts("-1");
			continue;
		}
		nxt[n+1]=n+1;
		num[1]=1;
		for(i=n;i>=1;i--)
		{
			if(num[i]>0)nxt[i]=i;
			else nxt[i]=nxt[i+1];
		}
		if(sol(2,1)==0)puts("-1");
	}
	return 0;
}


/*
I could not breath against the wall
The clash of light against the floor
They held me up against the wall
Too young to be ignored

Standing by a building
Leaks out information
Passive death of freedom gets me
Where's my security
Behind my back it goes
Early morning's coming
The risk that I am taking
Recall a song that I heard as a child
No more false immunity
Inside the blistering cold

My youth unknown to me
I'm walking by a wire
What's left from me to breathe
Cold smell of fire
One day I will be free
I was born to be no one
My youth unknown to me
I walked behind the sun

Emptiness within me
Exit signs within me
The one you're looking for is not here
Had no security
He will not come back
*/

//#pragma comment(linker, "/STACK:16777216")
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
//#include <memory.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

#define  INF 100000000
#define eps 1e-13
//#define M_PI 3.141592653589793
//#define mx 1000000000000ll
#define bs 1000000007

//#define free asdfasdfsdadsg
//#define szz 400
//#define pb push_back
#define MAXN 10000
#define free afdshjioey
//#define SIZE 60
#define bsize 2000

using namespace std;

long n;
vector<pair<long, long> > v;
long ar[300000];
long reach[300000][10];
long rspan,need1,need2;
long p,q;
long par[300000][10];
long ps,cur;
long tp,np,niters,m2,m1,qq;

int main(){
//freopen("ascii.in","r",stdin);
//freopen("ascii.out","w",stdout);
//freopen("C:/input.txt","r",stdin);
//freopen("C:/output.txt","w",stdout);
ios_base::sync_with_stdio(0);

cin>>n;

v.push_back(make_pair(0,0));
for (int i=1;i<=n;i++)
{
 cin>>ar[i];
 if (ar[i]>0)v.push_back(make_pair(ar[i],i));
}

reach[0][5]=1;

for (int i=1;i<v.size();i++)
if (v[i].first<v[i-1].first){cout<<-1<<endl;return 0;}

for (int i=0;i+1<v.size();i++)
 {
  rspan=v[i+1].second-v[i].second;
  for (int op=1;op<=5;op++)
    for (int np=1;np<=5;np++)
     {
      if (reach[i][op]==0)continue;
      if (v[i+1].first==v[i].first&&np<=op)continue;
      if (v[i+1].first==v[i].first)
       {
        need1=need2=np-op;
       }
       else
       {
        need1=(5-op)+(v[i+1].first-v[i].first-1)*5+np;
        need2=(v[i+1].first-v[i].first-1)*2+np;
        if (op==1)need2++;

  //      cout<<need1<<" "<<need2<<endl;

       }
      if (rspan>=need2&&rspan<=need1)
      {
       reach[i+1][np]=1;
       par[i+1][np]=op;
      }
     }
 }

/*for (int i=0;i<v.size();i++)
{
    cout<<v[i].first<<"^^"<<v[i].second<<endl;
    for (int j=0;j<=5;j++)
    cout<<reach[i][j]<<" ";
    cout<<endl;
}*/

if (v[v.size()-1].second==n)reach[v.size()-1][1]=0;
if (v[v.size()-1].second==n-1)reach[v.size()-1][5]=0;
if (v[v.size()-1].second==n-2)reach[v.size()-1][1]=0;
p=v.size()-1;

q=1;
while (q<=5&&reach[p][q]==0)++q;
if (q==6){cout<<-1<<endl;return 0;}

/*for (int j=1;j<=5;j++)
cout<<reach[p][j]<<" ";
cout<<endl;
*/
//cout<<q<<endl; // gt t+

// tail
if (v[p].second==n-1)ar[n]=ar[n-1];
else
{
    ps=v[p].second;
    if (q==1){++ps;ar[ps]=ar[ps-1];}
    cur=ar[ps]+1;
    ++ps;
    while (ps+2<=n)
    {
     if (ps+2==n){ar[ps]=ar[ps+1]=ar[ps+2]=cur;++cur;ps+=3;}
     else{ar[ps]=ar[ps+1]=cur;cur++;ps+=2;}
    }
    if (ps<n){ar[ps]=cur;ar[ps+1]=cur;cur++;ps+=2;}
}

/*
for (int i=1;i<=n;i++)
{
    if (i-1)cout<<" ";
    cout<<ar[i];
}
cout<<endl;
*/

for (int i=v.size()-1;i;--i)
{
    if (v[i-1].first==v[i].first){for (int j=v[i-1].second+1;j<v[i].second;j++)
    {
                                      ar[j]=ar[v[i-1].second];
    }
    q=par[i][q];continue;
    }
    else
    {
        for (int j=q;j>=1;j--)
        ar[v[i].second-q+j]=ar[v[i].second];
        tp=v[i].second-q;

        np=par[i][q];

        niters=v[i].first-v[i-1].first-1;
        for (;niters;--niters)
        {
         /*m1=(niters-1)*2;
         if (np==1)++m1;*/
         m2=(niters-1)*5+(5-np);
         qq=2;
         while (tp-qq>(v[i-1].second+m2))++qq;
         for (int j=tp-qq+1;j<=tp;j++)
          ar[j]=v[i-1].first+niters;
         tp=tp-qq;
        }
        for (int j=v[i-1].second;j<=tp;j++)
        ar[j]=v[i-1].first;

        q=np;

    }
}
cout<<ar[n]<<endl;
for (int i=1;i<=n;i++)
if (ar[i]==0)ar[i]=1;

for (int i=1;i<=n;i++)
{
    if (i-1)cout<<" ";
    cout<<ar[i];
}
cout<<endl;

cin.get();cin.get();
return 0;}