#pragma comment(linker, "/STACK:600000000")
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>

#include <ctime>
#include <cassert>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef vector <int> vi;
#define sz(s) int((s).size())

typedef long double ldb;
typedef long long int64;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;

#define y0 wwwwwww
#define y1 qqqqqqq
#define next NEXT
#define prev PREV
#define forn(i, n) for (int i = 0; i < (int) n; i++)
#define ford(i, n) for (int i = (int) n - 1; i >= 0; i--)
#define seta(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define last(a) a[a.size() - 1]
#define mp make_pair
#define fs first
#define sc second

template <class T> T sqr(T x) { return x * x; }

double const pi = 3.1415926535897932384626433832795;
int const inf = (int) 1e9;
int64 const inf64 = (int64) 4e18;
const string name = "e";

const int NMAX = 200010;

int n, a[NMAX], d[NMAX][6], ans[NMAX], prev[NMAX][6];
vector<pii> q;

vector<pii> get_opt(int idx1, int col1, int sh1, int idx2, int col2, int sh2, bool& answer) {
	vector<pii> res;
	answer = 0;
	if (col1 > col2 || (col1 == col2 && sh1 >= sh2)) return res;
	if (col1 == col2) {
		if (sh2 - sh1 == idx2 - idx1) {
			forn(i, sh2 - sh1 - 1)
				res.pb(mp(idx1 + i + 1, col1));
			answer = 1;
		}
		return res;
	}

	vector<pii> RES;
	forn(i, sh2 - 1)
		RES.pb(mp(idx2 - i - 1, col2));
	idx2 -= sh2, col2--;
	for (int i = max(0, 2 - sh1); i < 6 - sh1; ++i) {
		res = RES;
		forn(j, i)
			res.pb(mp(idx1 + j + 1, col1));
		int newidx1 = idx1 + i + 1;
		int newcol1 = col1 + 1;

		if (col2 == col1 - 1) {
			if (idx2 == newidx1 - 1) {
				answer = 1;
				return res;
			}
			continue;
		}
		int delta_idx = idx2 - newidx1 + 1;
		int delta_col = col2 - newcol1 + 1;
		if (2 * delta_col <= delta_idx && delta_idx <= 5 * delta_col) {
			int sz, num;
			if (delta_col == 0) sz = 0, num = 0;
			else sz = delta_idx / delta_col, num = delta_col - delta_idx % delta_col;
			forn(j, num) {
				forn(f, sz)
					res.pb(mp(newidx1++, newcol1));
				newcol1++;
			}
			forn(j, delta_col - num) {
				forn(f, sz + 1)
					res.pb(mp(newidx1++, newcol1));
				newcol1++;
			}
			answer = 1;
			return res;
		}
	}
	res.clear();
	return res;
}

int may(int idx1, int col1, int sh1, int idx2, int col2, int sh2) {
	bool answer = 0;
	vector<pii> ept = get_opt(idx1, col1, sh1, idx2, col2, sh2, answer);
	return answer;
}

void rec(int x, int y) {
	ans[q[x].fs] = q[x].sc;
	if (q[x].sc == 1925 && x > 0 && q[x - 1].sc == 1925 && y < prev[x][y]) exit(1);
	if (x == 0) return;
	rec(x - 1, prev[x][y]);
	bool unused = 0;
	vector<pii> ept = get_opt(q[x - 1].fs, q[x - 1].sc, prev[x][y], q[x].fs, q[x].sc, y, unused);

	forn(i, ept.size())
		ans[ept[i].fs] = ept[i].sc;
}

int main() {
//	freopen((name + ".in").data(), "r", stdin);
//	freopen((name + ".out").data(), "w", stdout);

	cin >> n;
	q.pb(mp(0, 1));
	forn(i, n) {
		scanf("%d", &a[i]);
		if (i == 0 && a[i] != 0) {
			if (a[i] != 1) {
				cout << -1 << endl;
				return 0;
			}
		}
		else if (a[i] != 0) q.pb(mp(i, a[i]));
	}
	seta(d, 0);
	d[0][1] = 1;
	forn(i, q.size() - 1)
		forn(j, 6)
			if (j && d[i][j])
				forn(f, 6) {
					if (f && may(q[i].fs, q[i].sc, j, q[i + 1].fs, q[i + 1].sc, f))
						d[i + 1][f] = 1;
						prev[i + 1][f] = j;
				}

	ford(j, 6)
		if (d[q.size() - 1][j] && (j >= 2 || q[q.size() - 1].fs != n - 1) && (j <= 4 || q[q.size() - 1].fs != n - 2)) {
			rec(q.size() - 1, j);
			int now = q[q.size() - 1].fs + 1;
			if (j == 1)
				ans[now++] = q[q.size() - 1].sc;
			while (now < n) {
				if (now == n - 1) {
					ans[now] = ans[now - 1];
					now++;
				}
				else {
					ans[now] = ans[now - 1] + 1;
					ans[now + 1] = ans[now - 1] + 1;
					now += 2;
				}
			}
			cout << ans[n - 1] << endl;
			forn(i, n) {
				if (i) printf(" ");
				printf("%d", ans[i]);
			}
			cout << endl;
			exit(0);
		}
	cout << -1 << endl;

	return 0;
}


#pragma comment(linker, "/STACK:600000000")
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>

#include <ctime>
#include <cassert>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef vector <int> vi;
#define sz(s) int((s).size())

typedef long double ldb;
typedef long long int64;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;

#define y0 wwwwwww
#define y1 qqqqqqq
#define next NEXT
#define prev PREV
#define forn(i, n) for (int i = 0; i < (int) n; i++)
#define ford(i, n) for (int i = (int) n - 1; i >= 0; i--)
#define seta(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define last(a) a[a.size() - 1]
#define mp make_pair
#define fs first
#define sc second

template <class T> T sqr(T x) { return x * x; }

double const pi = 3.1415926535897932384626433832795;
int const inf = (int) 1e9;
int64 const inf64 = (int64) 4e18;
const string name = "e";

const int NMAX = 200010;

int n, a[NMAX], d[NMAX][6], ans[NMAX], prev[NMAX][6];
vector<pii> q;

vector<pii> get_opt(int idx1, int col1, int sh1, int idx2, int col2, int sh2, bool& answer) {
	vector<pii> res;
	answer = 0;
	if (col1 == col2) {
		if (sh2 - sh1 == idx2 - idx1) {
			forn(i, sh2 - sh1 - 1)
				res.pb(mp(idx1 + i + 1, col1));
			answer = 1;
		}
		return res;
	}

	vector<pii> RES;
	forn(i, sh2 - 1)
		RES.pb(mp(idx2 - i - 1, col2));
	idx2 -= sh2, col2--;
	for (int i = max(0, 2 - sh1); i < 6 - sh1; ++i) {
		res = RES;
		forn(j, i)
			res.pb(mp(idx1 + j + 1, col1));
		int newidx1 = idx1 + i + 1;
		int newcol1 = col1 + 1;

		if (col2 == col1 - 1) {
			if (idx2 == newidx1 - 1) {
				answer = 1;
				return res;
			}
			continue;
		}
		int delta_idx = idx2 - newidx1 + 1;
		int delta_col = col2 - newcol1 + 1;
		if (2 * delta_col <= delta_idx && delta_idx <= 5 * delta_col) {
			int sz, num;
			if (delta_col == 0) sz = 0, num = 0;
			else sz = delta_idx / delta_col, num = delta_col - delta_idx % delta_col;
			forn(j, num) {
				forn(f, sz)
					res.pb(mp(newidx1++, newcol1));
				newcol1++;
			}
			forn(j, delta_col - num) {
				forn(f, sz + 1)
					res.pb(mp(newidx1++, newcol1));
				newcol1++;
			}
			answer = 1;
			return res;
		}
	}
	res.clear();
	return res;
}

int may(int idx1, int col1, int sh1, int idx2, int col2, int sh2) {
	bool answer = 0;
	vector<pii> ept = get_opt(idx1, col1, sh1, idx2, col2, sh2, answer);
	return answer;
}

void rec(int x, int y) {
	ans[q[x].fs] = q[x].sc;
	if (x == 0) return;
	rec(x - 1, prev[x][y]);
	bool unused = 0;
	vector<pii> ept = get_opt(q[x - 1].fs, q[x - 1].sc, prev[x][y], q[x].fs, q[x].sc, y, unused);
	forn(i, ept.size())
		ans[ept[i].fs] = ept[i].sc;
}

int main() {
//	freopen((name + ".in").data(), "r", stdin);
//	freopen((name + ".out").data(), "w", stdout);

	cin >> n;
	q.pb(mp(0, 1));
	forn(i, n) {
		scanf("%d", &a[i]);
		if (i == 0 && a[i] != 0) {
			if (a[i] != 1) {
				cout << -1 << endl;
				return 0;
			}
		}
		else if (a[i] != 0) q.pb(mp(i, a[i]));
	}
	seta(d, 0);
	d[0][1] = 1;
	forn(i, q.size() - 1)
		forn(j, 6)
			if (j && d[i][j])
				forn(f, 6) {
					if (f && may(q[i].fs, q[i].sc, j, q[i + 1].fs, q[i + 1].sc, f))
						d[i + 1][f] = 1;
						prev[i + 1][f] = j;
				}

	ford(j, 6)
		if (d[q.size() - 1][j] && (j >= 2 || q[q.size() - 1].fs != n - 1)) {
			rec(q.size() - 1, j);
			int now = q[q.size() - 1].fs + 1;
			if (j == 1)
				ans[now++] = q[q.size() - 1].sc;
			while (now < n) {
				if (now == n - 1) {
					ans[now] = ans[now - 1];
					now++;
				}
				else {
					ans[now] = ans[now - 1] + 1;
					ans[now + 1] = ans[now - 1] + 1;
					now += 2;
				}
			}
			cout << ans[n - 1] << endl;
			forn(i, n) {
				if (i) printf(" ");
				printf("%d", ans[i]);
			}
			cout << endl;
			exit(0);
		}
	cout << -1 << endl;

	return 0;
}


#pragma comment(linker, "/STACK:600000000")
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
#include <stack>
#include <list>

#include <ctime>
#include <cassert>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef vector <int> vi;
#define sz(s) int((s).size())

typedef long double ldb;
typedef long long int64;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;

#define y0 wwwwwww
#define y1 qqqqqqq
#define next NEXT
#define prev PREV
#define forn(i, n) for (int i = 0; i < (int) n; i++)
#define ford(i, n) for (int i = (int) n - 1; i >= 0; i--)
#define seta(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define last(a) a[a.size() - 1]
#define mp make_pair
#define fs first
#define sc second

template <class T> T sqr(T x) { return x * x; }

double const pi = 3.1415926535897932384626433832795;
int const inf = (int) 1e9;
int64 const inf64 = (int64) 4e18;
const string name = "e";

const int NMAX = 200010;

int n, a[NMAX], d[NMAX][6], ans[NMAX], prev[NMAX][6];
vector<pii> q;

vector<pii> get_opt(int idx1, int col1, int sh1, int idx2, int col2, int sh2, bool& answer) {
	vector<pii> res;
	answer = 0;
	if (col1 == col2) {
		if (sh2 - sh1 == idx2 - idx1) {
			forn(i, sh2 - sh1 - 1)
				res.pb(mp(idx1 + i + 1, col1));
			answer = 1;
		}
		return res;
	}

	vector<pii> RES;
	forn(i, sh2 - 1)
		RES.pb(mp(idx2 - i - 1, col2));
	idx2 -= sh2, col2--;
	for (int i = max(0, 2 - sh1); i < 6 - sh1; ++i) {
		res = RES;
		forn(j, i)
			res.pb(mp(idx1 + j + 1, col1));
		int newidx1 = idx1 + i + 1;
		int newcol1 = col1 + 1;

		if (col2 == col1 - 1) {
			if (idx2 == newidx1 - 1) {
				answer = 1;
				return res;
			}
			continue;
		}
		int delta_idx = idx2 - newidx1 + 1;
		int delta_col = col2 - newcol1 + 1;
		if (2 * delta_col <= delta_idx && delta_idx <= 5 * delta_col) {
			int sz, num;
			if (delta_col == 0) sz = 0, num = 0;
			else sz = delta_idx / delta_col, num = delta_col - delta_idx % delta_col;
			forn(j, num) {
				forn(f, sz)
					res.pb(mp(newidx1++, newcol1));
				newcol1++;
			}
			forn(j, delta_col - num) {
				forn(f, sz + 1)
					res.pb(mp(newidx1++, newcol1));
				newcol1++;
			}
			answer = 1;
			return res;
		}
	}
	res.clear();
	return res;
}

int may(int idx1, int col1, int sh1, int idx2, int col2, int sh2) {
	bool answer = 0;
	vector<pii> ept = get_opt(idx1, col1, sh1, idx2, col2, sh2, answer);
	return answer;
}

void rec(int x, int y) {
	ans[q[x].fs] = q[x].sc;
	if (q[x].sc == 1925) exit(1);
	if (x == 0) return;
	rec(x - 1, prev[x][y]);
	bool unused = 0;
	vector<pii> ept = get_opt(q[x - 1].fs, q[x - 1].sc, prev[x][y], q[x].fs, q[x].sc, y, unused);

	forn(i, ept.size())
		ans[ept[i].fs] = ept[i].sc;
}

int main() {
//	freopen((name + ".in").data(), "r", stdin);
//	freopen((name + ".out").data(), "w", stdout);

	cin >> n;
	q.pb(mp(0, 1));
	forn(i, n) {
		scanf("%d", &a[i]);
		if (i == 0 && a[i] != 0) {
			if (a[i] != 1) {
				cout << -1 << endl;
				return 0;
			}
		}
		else if (a[i] != 0) q.pb(mp(i, a[i]));
	}
	seta(d, 0);
	d[0][1] = 1;
	forn(i, q.size() - 1)
		forn(j, 6)
			if (j && d[i][j])
				forn(f, 6) {
					if (f && may(q[i].fs, q[i].sc, j, q[i + 1].fs, q[i + 1].sc, f))
						d[i + 1][f] = 1;
						prev[i + 1][f] = j;
				}

	ford(j, 6)
		if (d[q.size() - 1][j] && (j >= 2 || q[q.size() - 1].fs != n - 1) && (j <= 4 || q[q.size() - 1].fs != n - 2)) {
			rec(q.size() - 1, j);
			int now = q[q.size() - 1].fs + 1;
			if (j == 1)
				ans[now++] = q[q.size() - 1].sc;
			while (now < n) {
				if (now == n - 1) {
					ans[now] = ans[now - 1];
					now++;
				}
				else {
					ans[now] = ans[now - 1] + 1;
					ans[now + 1] = ans[now - 1] + 1;
					now += 2;
				}
			}
			cout << ans[n - 1] << endl;
			forn(i, n) {
				if (i) printf(" ");
				printf("%d", ans[i]);
			}
			cout << endl;
			exit(0);
		}
	cout << -1 << endl;

	return 0;
}


#include <iostream>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <memory.h>

using namespace std;

#define fs first
#define sc second

typedef long long li;

int l[200005], r[200005];
pair < pair < int, int > , int > p[200005];
int a[200005];
int n;
int dp[100005][10];
int ans[200005];
int cur[200005][10];

bool check(int l, int r, int L, int R){
	if(2 * (R - L + 1) <= r - l + 1 && r - l + 1 <= 5 * (R - L + 1)) return true;
	return false;
}

void full(int l, int r, int L, int R){
	if(l > r) return;
	if(l > n - 1) return;
	int x = (r - l + 1) / (R - L + 1);
	int y = (r - l + 1) % (R - L + 1);
	int i;
	int k = 0;
	for(i = l; i <= r; i += x){
		for(int j = 0; j < x; j++)
			ans[i + j] = L + k;
		k++;
		if(k == R - L + 1 - y)
			break;
	}
	for(int i1 = i + x; i1 <= r; i1 += x + 1){
		for(int j = 0; j < x + 1; j++)
			ans[i1 + j] = L + k;
		k++;
	}
}

int main(){
	memset(l, 255, sizeof(l));
	memset(dp, 255, sizeof(dp));
	cin >> n;
	int i;
	for(i = 0; i < n; i++){
		cin >> a[i];
		if(a[i] != 0){
			r[a[i]] = i;
			if(l[a[i]] == -1)
				l[a[i]] = i;
		}
	}
	p[0].fs.fs = -1;
	p[0].fs.sc = -1;
	p[0].sc = 0;
	dp[0][0] = 0;
	int k = 1;
	for(i = 0; i <= 1e5; i++){
		if(l[i] != -1){
			p[k].fs.fs = l[i];
			p[k].fs.sc = r[i];
			p[k].sc = i;
			k++;
			if(r[i] - l[i] > 4){
				cout << -1;
				return 0;
			}
		}
	}
	sort(p, p + k);
	for(i = 0; i < k - 1; i++)
		if(p[i + 1].fs.sc <= p[i].fs.fs){
			cout << -1;
			return 0;
		}
	if(k == 0){
		int z = 1;
		for(i = 0; i < n; i ++){
			cout << z << " ";
			if(i % 2 == 1 && i != n - 2)
				z++;
		}
	}
	for(i = 1; i < k; i++)
		for(int z = 0; z < 5; z++){
			if(p[i].fs.fs + z < p[i].fs.sc)
				continue;
			for(int z1 = 1; z1 < 5; z1++){
				if(p[i].fs.fs < p[i].fs.fs + z - z1)
					continue;
				if(p[i].fs.fs + z - z1 < 0 || p[i].fs.fs + z >= n)
					continue;
				for(int j = 0; j < 5; j++)
					if(dp[i - 1][j] != -1 && check(p[i - 1].fs.fs + j + 1, p[i].fs.fs + z - z1 - 1, p[i - 1].sc + 1, p[i].sc - 1)){
						dp[i][z] = j;
						cur[i][z] = p[i].fs.fs + z - z1;
					}
			}
		}
	int sum = p[k - 1].sc;
	for(i = 0; i < 5; i++)
		if(dp[k - 1][i] != -1 && n - 1  - (p[k - 1].fs.fs + i) != 1){
			full(p[k - 1].fs.fs + i + 1, n - 1, p[k - 1].sc + 1, ((n - 1) - (p[k - 1].fs.fs + i + 1) + 1) / 2 + p[k - 1].sc);
			sum += ((n - 1) - (p[k - 1].fs.fs + i + 1) + 1) / 2;
			break;
		}
	/*for(i = 0; i < 15; i++)
		cout << ans[i] << " ";*/
	int z = -1;
	for(i = 0; i < 5; i++)
		if(dp[k - 1][i] != -1 && n - 1  - (p[k - 1].fs.fs + i) != 1){
			z = i;
			break;
		}
	if(z == -1){
		cout << -1;
		return 0;
	}
	i = k - 1;
	while(i > 0){
	//	cout << cur[i] << "\n";
		full(p[i - 1].fs.fs + dp[i][z] + 1, cur[i][z] - 1, p[i - 1].sc + 1, p[i].sc - 1);
		for(int j = cur[i][z]; j <= p[i].fs.fs + z; j++)
			ans[j] = p[i].sc;
		z = dp[i][z];
		i--;
	}
	cout << sum << "\n";
	for(i = 0; i < n; i++)
		cout << ans[i] << " ";
	return 0;
}


#include <iostream>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <memory.h>

using namespace std;

#define fs first
#define sc second

typedef long long li;

int l[200005], r[200005];
pair < pair < int, int > , int > p[200005];
int a[200005];
int n;
int dp[100005][10];
int ans[200005];
int cur[200005][10];

bool check(int l, int r, int L, int R){
	if(2 * (R - L + 1) <= r - l + 1 && r - l + 1 <= 5 * (R - L + 1)) return true;
	return false;
}

void full(int l, int r, int L, int R){
	if(l > r) return;
	if(l > n - 1) return;
	int x = (r - l + 1) / (R - L + 1);
	int y = (r - l + 1) % (R - L + 1);
	int i;
	int k = 0;
	for(i = l; i <= r; i += x){
		for(int j = 0; j < x; j++)
			ans[i + j] = L + k;
		k++;
		if(k == R - L + 1 - y)
			break;
	}
	for(int i1 = i + x; i1 <= r; i1 += x + 1){
		for(int j = 0; j < x + 1; j++)
			ans[i1 + j] = L + k;
		k++;
	}
}

int main(){
	memset(l, 255, sizeof(l));
	memset(dp, 255, sizeof(dp));
	cin >> n;
	int i;
	for(i = 0; i < n; i++){
		cin >> a[i];
		if(a[i] != 0){
			r[a[i]] = i;
			if(l[a[i]] == -1)
				l[a[i]] = i;
		}
	}
	p[0].fs.fs = -1;
	p[0].fs.sc = -1;
	p[0].sc = 0;
	dp[0][0] = 0;
	int k = 1;
	for(i = 0; i <= 1e5; i++){
		if(l[i] != -1){
			p[k].fs.fs = l[i];
			p[k].fs.sc = r[i];
			p[k].sc = i;
			k++;
			if(r[i] - l[i] > 4){
				cout << -1;
				return 0;
			}
		}
	}
	sort(p, p + k);
	for(i = 0; i < k - 1; i++)
		if(p[i + 1].fs.sc <= p[i].fs.fs){
			cout << -1;
			return 0;
		}
	if(k == 0){
		int z = 1;
		for(i = 0; i < n; i ++){
			cout << z << " ";
			if(i % 2 == 1 && i != n - 2)
				z++;
		}
	}
	for(i = 1; i < k; i++)
		for(int z = 0; z < 5; z++){
			if(p[i].fs.fs + z < p[i].fs.sc)
				continue;
			for(int z1 = 1; z1 < 5; z1++){
				if(p[i].fs.fs < p[i].fs.fs + z - z1)
					continue;
				if(p[i].fs.fs + z - z1 < 0 || p[i].fs.fs + z >= n)
					continue;
				for(int j = 0; j < 5; j++)
					if(dp[i - 1][j] != -1 && check(p[i - 1].fs.fs + j + 1, p[i].fs.fs + z - z1 - 1, p[i - 1].sc + 1, p[i].sc - 1)){
						dp[i][z] = j;
						cur[i][z] = p[i].fs.fs + z - z1;
					}
			}
		}
	for(i = 0; i < 5; i++)
		if(dp[k - 1][i] != -1 && n - 1  - (p[k - 1].fs.fs + i) != 1){
			full(p[k - 1].fs.fs + i + 1, n - 1, p[k - 1].sc + 1, ((n - 1) - (p[k - 1].fs.fs + i + 1) + 1) / 2 + p[k - 1].sc);
			break;
		}
	/*for(i = 0; i < 15; i++)
		cout << ans[i] << " ";*/
	int z = -1;
	for(i = 0; i < 5; i++)
		if(dp[k - 1][i] != -1 && n - 1  - (p[k - 1].fs.fs + i) != 1){
			z = i;
			break;
		}
	if(z == -1){
		cout << -1;
		return 0;
	}
	i = k - 1;
	while(i > 0){
	//	cout << cur[i] << "\n";
		full(p[i - 1].fs.fs + dp[i][z] + 1, cur[i][z] - 1, p[i - 1].sc + 1, p[i].sc - 1);
		for(int j = cur[i][z]; j <= p[i].fs.fs + z; j++)
			ans[j] = p[i].sc;
		z = dp[i][z];
		i--;
	}
	for(i = 0; i < n; i++)
		cout << ans[i] << " ";
	return 0;
}


//*
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#define MOD 1000000007
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

int n;
int a[300000];
int b[300000][2];
int bi;

int d[300000][5];
int p[300000][5][2];

int dab[300000];

void path(int x, int y)
{
    int i, j, k;
    if(x == 0 && y == 0)
    {
        return;
    }
    int su1, su2, start, end;
    su1=a[p[x][y][0]]+1;
    start=p[x][y][0]+p[x][y][1]+1;
    su2=a[x];
    end=x+y;
    int length, gatsu;
    length=end-start+1, gatsu=su2-su1+1;
    for(i=2;i<=4;i++)
    {
        int low, high;
        if(length >= gatsu*i && length <= gatsu*(i+1))
        {
            high=length-gatsu*i;
            low=gatsu-high;
            int xx=su1;
            for(j=0;j<low;j++)
            {
                for(k=0;k<i;k++)
                {
                    dab[start+j*i+k]=xx;
                }
                xx++;
            }
            int xstart=start+low*i;
            for(j=0;j<high;j++)
            {
                for(k=0;k<i+1;k++)
                {
                    dab[xstart+j*(i+1)+k]=xx;
                }
                xx++;
            }
        }
    }
    path(p[x][y][0], p[x][y][1]);
}

int main()
{
    int i, j, k;
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i+1]);
        if(a[i+1] != 0)
        {
            b[bi][0]=a[i+1];
            b[bi][1]=i+1;
            bi++;
        }
    }
    d[0][0]=1;
    int lastd=0;
    for(i=1;i<=n;i++)
    {
        if(!a[i]) continue;
        for(j=0;j<5;j++)
        {
            if(!d[lastd][j]) continue;
            int min=lastd+j+(a[i]-a[lastd])*2;
            int max=lastd+j+(a[i]-a[lastd])*5;
            for(k=0;k<5;k++)
            {
                if(i+k >= min && i+k <= max)
                {
                    d[i][k]=1;
                    p[i][k][0]=lastd;
                    p[i][k][1]=j;
                }
            }
        }
        lastd=i;
    }
    int flag=0;
    for(i=0;i<5;i++)
    {
        if(lastd+i > n) break;
        if(d[lastd][i] == 1)
        {
            flag=1;
            break;
        }
    }
    if(flag == 0)
    {
        printf("-1");
        return 0;
    }
    if(lastd+i == n-1)
    {
        if(i == 4)
        {
            printf("-1");
            return 0;
        }
        dab[lastd+i]=a[lastd];
        dab[lastd+i+1]=a[lastd];
        path(lastd, i);
    }
    else
    {
        path(lastd, i);
        int xx=a[lastd];
        for(j=0;;j++)
        {
            //lastd+i+j*2+1, lastd+i+j*2+2
            if(lastd+i+j*2+2 > n) break;
            xx++;
            dab[lastd+i+j*2+1]=xx;
            dab[lastd+i+j*2+2]=xx;
        }
        if(dab[n] == 0)
        {
            dab[n]=xx;
        }
    }
    printf("%d\n", dab[n]);
    for(i=1;i<=n;i++) printf("%d ", dab[i]);
    return 0;
}
//*/


//*
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#define MOD 1000000007
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

int n;
int a[300000];
int b[300000][2];
int bi;

int d[300000][5];
int p[300000][5][2];

int dab[300000];

void path(int x, int y)
{
    int i, j, k, l;
    if(x == 0 && y == 0)
    {
        return;
    }
    int su1, su2, start, end;
    su1=a[p[x][y][0]]+1;
    start=p[x][y][0]+p[x][y][1]+1;
    su2=a[x];
    end=x+y;
    for(i=x;i<=x+y;i++) dab[i]=a[x];
    su2=su2-1;
    end=x;
    for(l=y;l<=4;l++)
    {
        dab[end]=a[x];
        end--;
        int length, gatsu;
        int flag=0;
        length=end-start+1, gatsu=su2-su1+1;
        for(i=2;i<=4;i++)
        {
            int low, high;
            if(length >= gatsu*i && length <= gatsu*(i+1))
            {
                flag=1;
                high=length-gatsu*i;
                low=gatsu-high;
                int xx=su1;
                for(j=0;j<low;j++)
                {
                    for(k=0;k<i;k++)
                    {
                        dab[start+j*i+k]=xx;
                    }
                    xx++;
                }
                int xstart=start+low*i;
                for(j=0;j<high;j++)
                {
                    for(k=0;k<i+1;k++)
                    {
                        dab[xstart+j*(i+1)+k]=xx;
                    }
                    xx++;
                }
            }
        }
        if(flag == 1) break;
    }
    path(p[x][y][0], p[x][y][1]);
}

int main()
{
    int i, j, k;
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i+1]);
        if(a[i+1] != 0)
        {
            b[bi][0]=a[i+1];
            b[bi][1]=i+1;
            bi++;
        }
    }
    d[0][0]=1;
    int lastd=0;
    for(i=1;i<=n;i++)
    {
        if(!a[i]) continue;
        for(j=0;j<5;j++)
        {
            if(!d[lastd][j]) continue;
            int min=lastd+j+(a[i]-a[lastd]-1)*2+1;
            int max=lastd+j+(a[i]-a[lastd])*5;
            for(k=0;k<5;k++)
            {
                if(i == 18 && k == 4)
                {
                    i=i;
                }
                if(a[i+k] && a[i+k] != a[i]) break;
                if(i >= min && i+k <= max)
                {
                    d[i][k]=1;
                    p[i][k][0]=lastd;
                    p[i][k][1]=j;
                }
            }
        }
        lastd=i;
    }
    int flag=0;
    for(i=0;i<5;i++)
    {
        if(lastd+i > n) break;
        if(d[lastd][i] == 1)
        {
            flag=1;
            break;
        }
    }
    if(flag == 0)
    {
        printf("-1");
        return 0;
    }
    if(lastd+i == n-1)
    {
        if(i == 4)
        {
            printf("-1");
            return 0;
        }
        dab[lastd+i]=a[lastd];
        dab[lastd+i+1]=a[lastd];
        path(lastd, i);
    }
    else
    {
        path(lastd, i);
        int xx=a[lastd];
        for(j=0;;j++)
        {
            //lastd+i+j*2+1, lastd+i+j*2+2
            if(lastd+i+j*2+2 > n) break;
            xx++;
            dab[lastd+i+j*2+1]=xx;
            dab[lastd+i+j*2+2]=xx;
        }
        if(dab[n] == 0)
        {
            dab[n]=xx;
        }
    }
    printf("%d\n", dab[n]);
    for(i=1;i<=n;i++) printf("%d ", dab[i]);
    return 0;
}
//*/


//*
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#define MOD 1000000007
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

int n;
int a[300000];
int b[300000][2];
int bi;

int d[300000][5];
int p[300000][5][2];

int dab[300000];

void path(int x, int y)
{
    int i, j, k;
    if(x == 0 && y == 0)
    {
        return;
    }
    int su1, su2, start, end;
    su1=a[p[x][y][0]]+1;
    start=p[x][y][0]+p[x][y][1]+1;
    su2=a[x];
    end=x+y;
    int length, gatsu;
    length=end-start+1, gatsu=su2-su1+1;
    for(i=2;i<=4;i++)
    {
        int low, high;
        if(length >= gatsu*i && length <= gatsu*(i+1))
        {
            high=length-gatsu*i;
            low=gatsu-high;
            int xx=su1;
            for(j=0;j<low;j++)
            {
                for(k=0;k<i;k++)
                {
                    dab[start+j*i+k]=xx;
                }
                xx++;
            }
            int xstart=start+low*i;
            for(j=0;j<high;j++)
            {
                for(k=0;k<i+1;k++)
                {
                    dab[xstart+j*(i+1)+k]=xx;
                }
                xx++;
            }
        }
    }
    path(p[x][y][0], p[x][y][1]);
}

int main()
{
    int i, j, k;
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i+1]);
        if(a[i+1] != 0)
        {
            b[bi][0]=a[i+1];
            b[bi][1]=i+1;
            bi++;
        }
    }
    d[0][0]=1;
    int lastd=0;
    for(i=1;i<=n;i++)
    {
        if(!a[i]) continue;
        for(j=0;j<5;j++)
        {
            if(!d[lastd][j]) continue;
            int min=lastd+j+(a[i]-a[lastd])*2;
            int max=lastd+j+(a[i]-a[lastd])*5;
            for(k=0;k<5;k++)
            {
                if(a[i+k] && a[i+k] != a[i]) break;
                if(i+k >= min && i+k <= max)
                {
                    d[i][k]=1;
                    p[i][k][0]=lastd;
                    p[i][k][1]=j;
                }
            }
        }
        lastd=i;
    }
    int flag=0;
    for(i=0;i<5;i++)
    {
        if(lastd+i > n) break;
        if(d[lastd][i] == 1)
        {
            flag=1;
            break;
        }
    }
    if(flag == 0)
    {
        printf("-1");
        return 0;
    }
    if(lastd+i == n-1)
    {
        if(i == 4)
        {
            printf("-1");
            return 0;
        }
        dab[lastd+i]=a[lastd];
        dab[lastd+i+1]=a[lastd];
        path(lastd, i);
    }
    else
    {
        path(lastd, i);
        int xx=a[lastd];
        for(j=0;;j++)
        {
            //lastd+i+j*2+1, lastd+i+j*2+2
            if(lastd+i+j*2+2 > n) break;
            xx++;
            dab[lastd+i+j*2+1]=xx;
            dab[lastd+i+j*2+2]=xx;
        }
        if(dab[n] == 0)
        {
            dab[n]=xx;
        }
    }
    printf("%d\n", dab[n]);
    for(i=1;i<=n;i++) printf("%d ", dab[i]);
    return 0;
}
//*/


//*
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#define MOD 1000000007
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

int n;
int a[300000];
int b[300000][2];
int bi;

int d[300000][5];
int p[300000][5][2];

int dab[300000];

void path(int x, int y)
{
    int i, j, k;
    if(x == 0 && y == 0)
    {
        return;
    }
    int su1, su2, start, end;
    su1=a[p[x][y][0]]+1;
    start=p[x][y][0]+p[x][y][1]+1;
    su2=a[x];
    end=x+y;
    int length, gatsu;
    length=end-start+1, gatsu=su2-su1+1;
    for(i=2;i<=4;i++)
    {
        int low, high;
        if(length >= gatsu*i && length <= gatsu*(i+1))
        {
            high=length-gatsu*i;
            low=gatsu-high;
            int xx=su1;
            for(j=0;j<low;j++)
            {
                for(k=0;k<i;k++)
                {
                    dab[start+j*i+k]=xx;
                }
                xx++;
            }
            int xstart=start+low*i;
            for(j=0;j<high;j++)
            {
                for(k=0;k<i+1;k++)
                {
                    dab[xstart+j*(i+1)+k]=xx;
                }
                xx++;
            }
        }
    }
    path(p[x][y][0], p[x][y][1]);
}

int main()
{
    int i, j, k;
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i+1]);
        if(a[i+1] != 0)
        {
            b[bi][0]=a[i+1];
            b[bi][1]=i+1;
            bi++;
        }
    }
    d[0][0]=1;
    int lastd=0;
    for(i=1;i<=n;i++)
    {
        if(!a[i]) continue;
        for(j=0;j<5;j++)
        {
            if(!d[lastd][j]) continue;
            int min=lastd+j+(a[i]-a[lastd])*2;
            int max=lastd+j+(a[i]-a[lastd])*5;
            for(k=0;k<5;k++)
            {
                if(a[i+k] != a[i]) break;
                if(i+k >= min && i+k <= max)
                {
                    d[i][k]=1;
                    p[i][k][0]=lastd;
                    p[i][k][1]=j;
                }
            }
        }
        lastd=i;
    }
    int flag=0;
    for(i=0;i<5;i++)
    {
        if(lastd+i > n) break;
        if(d[lastd][i] == 1)
        {
            flag=1;
            break;
        }
    }
    if(flag == 0)
    {
        printf("-1");
        return 0;
    }
    if(lastd+i == n-1)
    {
        if(i == 4)
        {
            printf("-1");
            return 0;
        }
        dab[lastd+i]=a[lastd];
        dab[lastd+i+1]=a[lastd];
        path(lastd, i);
    }
    else
    {
        path(lastd, i);
        int xx=a[lastd];
        for(j=0;;j++)
        {
            //lastd+i+j*2+1, lastd+i+j*2+2
            if(lastd+i+j*2+2 > n) break;
            xx++;
            dab[lastd+i+j*2+1]=xx;
            dab[lastd+i+j*2+2]=xx;
        }
        if(dab[n] == 0)
        {
            dab[n]=xx;
        }
    }
    printf("%d\n", dab[n]);
    for(i=1;i<=n;i++) printf("%d ", dab[i]);
    return 0;
}
//*/


//*
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#define MOD 1000000007
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

int n;
int a[300000];
int b[300000][2];
int bi;

int d[300000][5];
int p[300000][5][2];

int dab[300000];

void path(int x, int y)
{
    int i, j, k, l;
    if(x == 0 && y == 0)
    {
        return;
    }
    int su1, su2, start, end;
    su1=a[p[x][y][0]]+1;
    start=p[x][y][0]+p[x][y][1]+1;
    su2=a[x];
    end=x+y;
    for(i=x;i<=x+y;i++) dab[i]=a[x];
    su2=su2-1;
    end=x;
    for(l=y;l<=4;l++)
    {
        dab[end]=a[x];
        end--;
        int length, gatsu;
        int flag=0;
        length=end-start+1, gatsu=su2-su1+1;
        for(i=2;i<=4;i++)
        {
            int low, high;
            if(length >= gatsu*i && length <= gatsu*(i+1))
            {
                flag=1;
                high=length-gatsu*i;
                low=gatsu-high;
                int xx=su1;
                for(j=0;j<low;j++)
                {
                    for(k=0;k<i;k++)
                    {
                        dab[start+j*i+k]=xx;
                    }
                    xx++;
                }
                int xstart=start+low*i;
                for(j=0;j<high;j++)
                {
                    for(k=0;k<i+1;k++)
                    {
                        dab[xstart+j*(i+1)+k]=xx;
                    }
                    xx++;
                }
            }
        }
        if(flag == 1) break;
    }
    path(p[x][y][0], p[x][y][1]);
}

int main()
{
    int i, j, k;
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i+1]);
        if(a[i+1] != 0)
        {
            b[bi][0]=a[i+1];
            b[bi][1]=i+1;
            bi++;
        }
    }
    d[0][0]=1;
    int lastd=0;
    for(i=1;i<=n;i++)
    {
        if(!a[i]) continue;
        for(j=0;j<5;j++)
        {
            if(!d[lastd][j]) continue;
            int min=lastd+j+(a[i]-a[lastd]-1)*2+1;
            int min2=lastd+j+(a[i]-a[lastd])*2;
            int max=lastd+j+(a[i]-a[lastd])*5;
            for(k=0;k<5;k++)
            {
                if(i == 18 && k == 4)
                {
                    i=i;
                }
                if(a[i+k] && a[i+k] != a[i]) break;
                if(i >= min && i+k >= min2 && i+k <= max)
                {
                    d[i][k]=1;
                    p[i][k][0]=lastd;
                    p[i][k][1]=j;
                }
            }
        }
        lastd=i;
    }
    int flag=0;
    for(i=0;i<5;i++)
    {
        if(lastd+i > n) break;
        if(d[lastd][i] == 1)
        {
            flag=1;
            break;
        }
    }
    if(flag == 0)
    {
        printf("-1");
        return 0;
    }
    if(lastd+i == n-1)
    {
        if(i == 4)
        {
            printf("-1");
            return 0;
        }
        dab[lastd+i]=a[lastd];
        dab[lastd+i+1]=a[lastd];
        path(lastd, i);
    }
    else
    {
        path(lastd, i);
        int xx=a[lastd];
        for(j=0;;j++)
        {
            //lastd+i+j*2+1, lastd+i+j*2+2
            if(lastd+i+j*2+2 > n) break;
            xx++;
            dab[lastd+i+j*2+1]=xx;
            dab[lastd+i+j*2+2]=xx;
        }
        if(dab[n] == 0)
        {
            dab[n]=xx;
        }
    }
    printf("%d\n", dab[n]);
    for(i=1;i<=n;i++) printf("%d ", dab[i]);
    return 0;
}
//*/


//*
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#define MOD 1000000007
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

int n;
int a[300000];
int b[300000][2];
int bi;

int d[300000][5];
int p[300000][5][2];

int dab[300000];

void path(int x, int y)
{
    int i, j, k, l;
    if(x == 0 && y == 0)
    {
        return;
    }
    int su1, su2, start, end;
    su1=a[p[x][y][0]]+1;
    start=p[x][y][0]+p[x][y][1]+1;
    su2=a[x];
    end=x+y;
    if(y != 0)
    {
        for(i=x;i<=x+y;i++) dab[i]=a[x];
        su2=su2-1;
        end=x;
        l=y;
    }
    else
    {
        for(i=x-1;i<=x+y;i++) dab[i]=a[x];
        su2=su2-1;
        end=x-1;
        l=y+1;
    }
    for(;l<=4;l++)
    {
        dab[end]=a[x];
        end--;
        int length, gatsu;
        int flag=0;
        length=end-start+1, gatsu=su2-su1+1;
        for(i=2;i<=4;i++)
        {
            int low, high;
            if(length >= gatsu*i && length <= gatsu*(i+1))
            {
                flag=1;
                high=length-gatsu*i;
                low=gatsu-high;
                int xx=su1;
                for(j=0;j<low;j++)
                {
                    for(k=0;k<i;k++)
                    {
                        dab[start+j*i+k]=xx;
                    }
                    xx++;
                }
                int xstart=start+low*i;
                for(j=0;j<high;j++)
                {
                    for(k=0;k<i+1;k++)
                    {
                        dab[xstart+j*(i+1)+k]=xx;
                    }
                    xx++;
                }
            }
        }
        if(flag == 1) break;
    }
    path(p[x][y][0], p[x][y][1]);
}

int main()
{
    int i, j, k;
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i+1]);
        if(a[i+1] != 0)
        {
            b[bi][0]=a[i+1];
            b[bi][1]=i+1;
            bi++;
        }
    }
    d[0][0]=1;
    int lastd=0;
    for(i=1;i<=n;i++)
    {
        if(!a[i]) continue;
        for(j=0;j<5;j++)
        {
            if(!d[lastd][j]) continue;
            int min;
            if(a[lastd] != a[i]) min=lastd+j+(a[i]-a[lastd]-1)*2+1;
            else min=-1;
            int min2=lastd+j+(a[i]-a[lastd])*2;
            int max=lastd+j+(a[i]-a[lastd])*5;
            for(k=0;k<5;k++)
            {
                if(i == 4 && k == 3)
                {
                    i=i;
                }
                if(a[i+k] && a[i+k] != a[i]) break;
                if(i >= min && i+k >= min2 && i+k <= max)
                {
                    d[i][k]=1;
                    p[i][k][0]=lastd;
                    p[i][k][1]=j;
                }
            }
        }
        lastd=i;
    }
    int flag=0;
    for(i=0;i<5;i++)
    {
        if(lastd+i > n) break;
        if(d[lastd][i] == 1)
        {
            flag=1;
            break;
        }
    }
    if(flag == 0)
    {
        printf("-1");
        return 0;
    }
    if(lastd+i == n-1)
    {
        if(i == 4)
        {
            printf("-1");
            return 0;
        }
        dab[lastd+i]=a[lastd];
        dab[lastd+i+1]=a[lastd];
        path(lastd, i);
    }
    else
    {
        path(lastd, i);
        int xx=a[lastd];
        for(j=0;;j++)
        {
            //lastd+i+j*2+1, lastd+i+j*2+2
            if(lastd+i+j*2+2 > n) break;
            xx++;
            dab[lastd+i+j*2+1]=xx;
            dab[lastd+i+j*2+2]=xx;
        }
        if(dab[n] == 0)
        {
            dab[n]=xx;
        }
    }
    printf("%d\n", dab[n]);
    for(i=1;i<=n;i++) printf("%d ", dab[i]);
    return 0;
}
//*/


#include <iostream>
#include <cstdio>
using namespace std;
const int N = 2*100000+5;
int n, i, a[N];
int top, st[N];
bool ok = true;

bool dp[N][7];
int b[N];
bool check(int er, int qa)
{
   if(er >= qa*2 && er <= qa*5)
   return 1;
   return 0;
}
void fil(int s, int f, int l, int r)
{
   for(; r >= l;-- r)
   {
      if(l==r)
      {
         for(int j = s; j <= f; ++ j)
         {
            b[j] = l;
         }
      }
      else
      {
         for(int qa = 1; qa <= 4; ++ qa)
         {
            if((f-qa) > s)
            {
               if(check((f-qa)-s, r-l))
               {
                  for(int j = f-qa; j <= f; ++ j)
                     b[j] = r;
                  f = f-qa-1;
                  break;
               }
            }
         }
      }
   }
}

int main()
{
//   freopen("input.txt", "r", stdin);

   scanf("%d", &n);
   for(int i = 1; i <= n; ++ i)
   {
      scanf("%d", &a[i]);
      if(i==1 && a[i]==0)
         a[i] = 1;
      else
      if(i==1 && a[i]!=1)
         ok = false;

      if(a[i])
      {
         ++ top;
         st[top] = i;
      }
   }
   if(!ok)
   {
      printf("-1\n");
      return 0;
   }
   /*if(top==0)
   {
      printf("%d\n", n/2);
      int cr = 0;
      for(int i = 1; i+1 <= n; i+=2)
      {
         ++ cr;
         printf("%d %d ", cr, cr);
      }
      if(n%2)
      printf("%d", cr);
      printf("\n");
      return 0;
   }*/
   //top >= 0
   for(int i = 2; i <= top; ++ i)
   {
      if(a[ st[i] ] < a[ st[i-1] ])
      {
         printf("-1\n");
         return 0;
      }
   }
   for(int i = 1; i <= 4; ++ i)
         dp[1][i] = 1;

   for(int i = 2; i <= top; ++ i)
   {
      //a[ st[i-1] ] <= a[ st[i] ];
      if(a[ st[i-1] ] < a[ st[i] ])
      {
         for(int j = 0; j <= 4; ++ j)//dp[i][j]
         {
            for(int v = 0; v <= 4; ++ v)
            {
               if(j+v+1 <= 5 && j+v+1 >= 2)
               {
                  for(int u = 0; u <= 4; ++ u)
                  {
                     if(st[i-1]+u < st[i]-v)
                     {
                        if(dp[i-1][u] && check((st[i]-v)-(st[i-1]+u)-1, a[ st[i] ]-a[ st[i-1] ]-1))
                        {
                           dp[i][j] = 1;
                        }
                     }
                  }
               }
            }
         }
      }
      else
         for(int j = 0; j <= 4; ++ j)
            if((st[i]-st[i-1]+1+j) <= 5)
               if(dp[i-1][ (st[i]-st[i-1]+j) ])
                  dp[i][j] = 1;
   }
   int pos = -1;
   for(int j = 0; j <= 4; ++ j)
      if(st[top]+j <= n && dp[ top ] [ j ] && n-(st[top]+j)!=1)
      {
         pos = j;
         break;
      }
   if(pos==-1)
   {
      printf("-1\n");
      return 0;
   }

   int er = n-(st[top]+pos);

   int res = a[ st[top] ]+er/2;

   printf("%d\n", res);
   if(st[top]!=n)
   {
      ++ top;
      st[top] = n;
      a[ n ] = res;
      dp[ top ][ 0 ] = 1;
   }
   int val = 0;
   for(int i = top; i >= 1; -- i)
   {
      if(i==1)
      {
         for(int j = 1; j <= 1+val; ++ j)
         {
            b[j] = 1;
         }
      }
      else
      {
         if(a[ st[i-1] ] < a[ st[i] ])
         {
            int j = val;
            for(int v = 0; v <= 4; ++ v)
            {
               if(j+v+1 <= 5 && j+v+1 >= 2)
               {
                  for(int u = 0; u <= 4; ++ u)
                  {
                     if(st[i-1]+u < st[i]-v)
                     {
                        if(dp[i-1][u] && check((st[i]-v)-(st[i-1]+u)-1, a[ st[i] ]-a[ st[i-1] ]-1))
                        {
                           for(int in = st[i]-v; in <= st[i]+j; ++ in)
                              b[in] = a[ st[i] ];

                           fil((st[i-1]+u)+1, (st[i]-v)-1, a[ st[i-1] ]+1, a[ st[i] ]-1);
                           val = u;
                           goto x;
                        }
                     }
                  }
               }
            }
            x:
            int pr = 0;
         }
         else
         {
            val = (st[i]-st[i-1]+val);
         }
      }
   }
   for(int i = 1; i <= n; ++ i)
   printf("%d ", b[i]);
   printf("\n");
   //st[1]==1;
   return 0;
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

// (づ°ω°)づﾐ★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public void Solve()
    {
        int n = ReadInt();
        var a = ReadIntArray();
        n = a.Length;

        var dp = new Tuple<int, int>[6,n + 1];
        dp[5, 0] = Tuple.Create(0, 0);

        int last = 0, lv = 0;
        for (int i = 1; i <= n; i++)
            if (a[i - 1] > 0)
            {
                int d = i - last;
                for (int j = 1; j < 6; j++)
                    if (dp[j, last] != null)
                        for (int k = 1; k < 6; k++)
                        {
                            bool ok = false;
                            if (a[i - 1] == lv)
                            {
                                if (k - j == d)
                                {
                                    ok = true;
                                }
                            }
                            else if (a[i - 1] == lv + 1)
                            {
                                int b = i - k;
                                if (b - last + j > 5 || b < last || j == 1 && b == last)
                                    continue;
                                ok = true;
                            }
                            else
                            {
                                int r = a[i - 1] - lv - 1;
                                for (int x = 0; j + x < 6; x++)
                                {
                                    if (j + x < 2)
                                        continue;
                                    int t = i - k - (last + x);
                                    if (r * 2 <= t && r * 5 >= t)
                                        ok = true;
                                }
                            }
                            if (ok)
                                dp[k, i] = Tuple.Create(j, last);

                        }

                last = i;
                lv = a[i - 1];
            }

        bool f = dp[2, last] != null || dp[3, last] != null || dp[4, last] != null;
        if (dp[1, last] != null && last != n)
            f = true;
        if (dp[5, last] != null && last != n - 1)
            f = true;
        if (!f)
        {
            Write(-1);
            return;
        }

        int cs = 0;
        for (int i = 1; i < 6; i++)
        {
            if (dp[i, last] != null)
            {
                if (i == 1 && last == n || i == 5 && last == n - 1)
                    continue;
                cs = Math.Max(i, cs);
            }
        }

        if (last == n - 1)
            a[n - 1] = lv;
        else
        {
            int p = last + 1;
            int h = cs + 1;
            for (; p <= n; p++)
            {
                if (h > 2 && p < n)
                {
                    lv++;
                    h = 1;
                }
                a[p - 1] = lv;
                h++;
            }
        }

        while (last > 0)
        {
            int ps = dp[cs, last].Item1;
            int pr = dp[cs, last].Item2;
            int v = pr == 0 ? 0 : a[pr - 1];

            if (v == a[last - 1])
            {
                for (int i = pr; i <= last; i++)
                    a[i - 1] = v;
            }
            else if (v == a[last - 1] - 1)
            {
                for (int i = 1; i < cs; i++)
                    a[last - 1 - i] = v + 1;
                for (int i = last - cs; i > pr; i--)
                    a[i - 1] = v;
            }
            else
            {
                bool done = false;
                int r = a[last - 1] - v - 1;
                for (int i = 0; !done && ps + i < 6; i++)
                {
                    if (ps + i == 1)
                        continue;
                    int t = last - cs - (pr + i);
                    if (r * 2 <= t && r * 5 >= t)
                    {
                        done = true;
                        int q = t / r;
                        int m = t % r;
                        int ptr = pr + i + 1;
                        for (int j = 0; j < r; j++)
                        {
                            int lim = j < m ? q + 1 : q;
                            for (int k = 0; k < lim; k++)
                            {
                                a[ptr - 1] = v + j + 1;
                                ptr++;
                            }
                        }
                        for (int j = 1; j <= i; j++)
                            a[pr + j - 1] = v;
                        for (int j = 1; j < cs; j++)
                            a[last - j - 1] = a[last - 1];
                    }
                }
            }

            cs = ps;
            last = pr;
        }

        Write(lv);
        WriteArray(a);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;

    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("cycle.in");
        //writer = new StreamWriter("cycle.out");
#endif
        try
        {
            //            var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //            thread.Start();
            //            thread.Join();
            new Solver().Solve();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            Console.WriteLine(ex);
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write

    private static Queue<string> currentLineTokens = new Queue<string>();

    private static string[] ReadAndSplitLine()
    {
        return reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public static string ReadToken()
    {
        while (currentLineTokens.Count == 0)
            currentLineTokens = new Queue<string>(ReadAndSplitLine());
        return currentLineTokens.Dequeue();
    }

    public static int ReadInt()
    {
        return int.Parse(ReadToken());
    }

    public static long ReadLong()
    {
        return long.Parse(ReadToken());
    }

    public static double ReadDouble()
    {
        return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
    }

    public static int[] ReadIntArray()
    {
        return ReadAndSplitLine().Select(int.Parse).ToArray();
    }

    public static long[] ReadLongArray()
    {
        return ReadAndSplitLine().Select(long.Parse).ToArray();
    }

    public static double[] ReadDoubleArray()
    {
        return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
    }

    public static int[][] ReadIntMatrix(int numberOfRows)
    {
        int[][] matrix = new int[numberOfRows][];
        for (int i = 0; i < numberOfRows; i++)
            matrix[i] = ReadIntArray();
        return matrix;
    }

    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows);
        int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = new int[numberOfRows];
            for (int j = 0; j < numberOfRows; j++)
                ret[i][j] = matrix[j][i];
        }
        return ret;
    }

    public static string[] ReadLines(int quantity)
    {
        string[] lines = new string[quantity];
        for (int i = 0; i < quantity; i++)
            lines[i] = reader.ReadLine().Trim();
        return lines;
    }

    public static void WriteArray<T>(IEnumerable<T> array)
    {
        writer.WriteLine(string.Join(" ", array));
    }

    public static void Write(params object[] array)
    {
        WriteArray(array);
    }

    public static void WriteLines<T>(IEnumerable<T> array)
    {
        foreach (var a in array)
            writer.WriteLine(a);
    }

    class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }

    #endregion
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

// (づ°ω°)づﾐ★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public void Solve()
    {
        int n = ReadInt();
        var a = ReadIntArray();

        var dp = new Tuple<int, int>[6,n + 1];
        dp[5, 0] = Tuple.Create(0, 0);

        int last = 0, lv = 0;
        for (int i = 1; i <= n; i++)
            if (a[i - 1] > 0)
            {
                int d = i - last;
                for (int j = 1; j < 6; j++)
                    if (dp[j, last] != null)
                        for (int k = 1; k < 6; k++)
                        {
                            bool ok = false;
                            if (a[i - 1] == lv)
                            {
                                if (k - j == d)
                                {
                                    ok = true;
                                }
                            }
                            else if (a[i - 1] == lv + 1)
                            {
                                int b = i - k;
                                if (b - last + j > 5 || b < last || j == 1 && b == last)
                                    continue;
                                ok = true;
                            }
                            else
                            {
                                int r = a[i - 1] - lv - 1;
                                for (int x = 0; j + x < 6; x++)
                                {
                                    if (j + x < 2)
                                        continue;
                                    int t = i - k - (last + x);
                                    if (r * 2 <= t && r * 5 >= t)
                                        ok = true;
                                }
                            }
                            if (ok)
                                dp[k, i] = Tuple.Create(j, last);

                        }

                last = i;
                lv = a[i - 1];
            }

        bool f = dp[2, last] != null || dp[3, last] != null || dp[3, last] != null;
        if (dp[1, last] != null && last != n)
            f = true;
        if (dp[5, last] != null && last != n - 1)
            f = true;
        if (!f)
        {
            Write(-1);
            return;
        }

        int cs = 0;
        for (int i = 1; i < 6; i++)
        {
            if (dp[i, last] != null)
            {
                if (i == 1 && last == n || i == 5 && last == n - 1)
                    continue;
                cs = Math.Max(i, cs);
            }
        }

        if (last == n - 1)
            a[n - 1] = lv;
        else
        {
            int p = last + 1;
            int h = cs;
            for (; p <= n; p++)
            {
                if (h > 2 && p < n)
                {
                    lv++;
                    h = 1;
                }
                a[p - 1] = lv;
                h++;
            }
        }

        while (last > 0)
        {
            int ps = dp[cs, last].Item1;
            int pr = dp[cs, last].Item2;
            int v = pr == 0 ? 0 : a[pr - 1];

            if (v == a[last - 1])
            {
                for (int i = pr; i <= last; i++)
                    a[i - 1] = v;
            }
            else if (v == a[last - 1] - 1)
            {
                for (int i = 1; i < cs; i++)
                    a[last - 1 - i] = v + 1;
                for (int i = last - cs; i > pr; i--)
                    a[i - 1] = v;
            }
            else
            {
                bool done = false;
                int r = a[last - 1] - v - 1;
                for (int i = 0; !done && ps + i < 6; i++)
                {
                    if (ps + i == 1)
                        continue;
                    int t = last - cs - (pr + i);
                    if (r * 2 <= t && r * 5 >= t)
                    {
                        done = true;
                        int q = t / r;
                        int m = t % r;
                        int ptr = pr + i + 1;
                        for (int j = 0; j < r; j++)
                        {
                            int lim = j < m ? q + 1 : q;
                            for (int k = 0; k < lim; k++)
                            {
                                a[ptr - 1] = v + j + 1;
                                ptr++;
                            }
                        }
                        for (int j = 1; j <= i; j++)
                            a[pr + j - 1] = v;
                        for (int j = 1; j < cs; j++)
                            a[last - j - 1] = a[last - 1];
                    }
                }
            }

            cs = ps;
            last = pr;
        }

        Write(lv);
        WriteArray(a);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;

    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("cycle.in");
        //writer = new StreamWriter("cycle.out");
#endif
        try
        {
            //            var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //            thread.Start();
            //            thread.Join();
            new Solver().Solve();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            Console.WriteLine(ex);
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write

    private static Queue<string> currentLineTokens = new Queue<string>();

    private static string[] ReadAndSplitLine()
    {
        return reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public static string ReadToken()
    {
        while (currentLineTokens.Count == 0)
            currentLineTokens = new Queue<string>(ReadAndSplitLine());
        return currentLineTokens.Dequeue();
    }

    public static int ReadInt()
    {
        return int.Parse(ReadToken());
    }

    public static long ReadLong()
    {
        return long.Parse(ReadToken());
    }

    public static double ReadDouble()
    {
        return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
    }

    public static int[] ReadIntArray()
    {
        return ReadAndSplitLine().Select(int.Parse).ToArray();
    }

    public static long[] ReadLongArray()
    {
        return ReadAndSplitLine().Select(long.Parse).ToArray();
    }

    public static double[] ReadDoubleArray()
    {
        return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
    }

    public static int[][] ReadIntMatrix(int numberOfRows)
    {
        int[][] matrix = new int[numberOfRows][];
        for (int i = 0; i < numberOfRows; i++)
            matrix[i] = ReadIntArray();
        return matrix;
    }

    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows);
        int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = new int[numberOfRows];
            for (int j = 0; j < numberOfRows; j++)
                ret[i][j] = matrix[j][i];
        }
        return ret;
    }

    public static string[] ReadLines(int quantity)
    {
        string[] lines = new string[quantity];
        for (int i = 0; i < quantity; i++)
            lines[i] = reader.ReadLine().Trim();
        return lines;
    }

    public static void WriteArray<T>(IEnumerable<T> array)
    {
        writer.WriteLine(string.Join(" ", array));
    }

    public static void Write(params object[] array)
    {
        WriteArray(array);
    }

    public static void WriteLines<T>(IEnumerable<T> array)
    {
        foreach (var a in array)
            writer.WriteLine(a);
    }

    class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }

    #endregion
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

// (づ°ω°)づﾐ★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public void Solve()
    {
        int n = ReadInt();
        var a = ReadIntArray();

        var dp = new Tuple<int, int>[6,n + 1];
        dp[5, 0] = Tuple.Create(0, 0);

        int last = 0, lv = 0;
        for (int i = 1; i <= n; i++)
            if (a[i - 1] > 0)
            {
                int d = i - last;
                for (int j = 1; j < 6; j++)
                    if (dp[j, last] != null)
                        for (int k = 1; k < 6; k++)
                        {
                            bool ok = false;
                            if (a[i - 1] == lv)
                            {
                                if (k - j == d)
                                {
                                    ok = true;
                                }
                            }
                            else if (a[i - 1] == lv + 1)
                            {
                                int b = i - k;
                                if (b - last + j > 5 || b < last || j == 1 && b == last)
                                    continue;
                                ok = true;
                            }
                            else
                            {
                                int r = a[i - 1] - lv - 1;
                                for (int x = 0; j + x < 6; x++)
                                {
                                    if (j + x < 2)
                                        continue;
                                    int t = i - k - (last + x);
                                    if (r * 2 <= t && r * 5 >= t)
                                        ok = true;
                                }
                            }
                            if (ok)
                                dp[k, i] = Tuple.Create(j, last);

                        }

                last = i;
                lv = a[i - 1];
            }

        bool f = dp[2, last] != null || dp[3, last] != null || dp[3, last] != null;
        if (dp[1, last] != null && last != n)
            f = true;
        if (dp[5, last] != null && last != n - 1)
            f = true;
        if (!f)
        {
            Write(-1);
            return;
        }

        int cs = 0;
        for (int i = 1; i < 6; i++)
        {
            if (dp[i, last] != null)
            {
                if (i == 1 && last == n || i == 5 && last == n - 1)
                    continue;
                cs = Math.Max(i, cs);
            }
        }

        if (last == n - 1)
            a[n - 1] = lv;
        else
        {
            int p = last + 1;
            int h = cs + 1;
            for (; p <= n; p++)
            {
                if (h > 2 && p < n)
                {
                    lv++;
                    h = 1;
                }
                a[p - 1] = lv;
                h++;
            }
        }

        while (last > 0)
        {
            int ps = dp[cs, last].Item1;
            int pr = dp[cs, last].Item2;
            int v = pr == 0 ? 0 : a[pr - 1];

            if (v == a[last - 1])
            {
                for (int i = pr; i <= last; i++)
                    a[i - 1] = v;
            }
            else if (v == a[last - 1] - 1)
            {
                for (int i = 1; i < cs; i++)
                    a[last - 1 - i] = v + 1;
                for (int i = last - cs; i > pr; i--)
                    a[i - 1] = v;
            }
            else
            {
                bool done = false;
                int r = a[last - 1] - v - 1;
                for (int i = 0; !done && ps + i < 6; i++)
                {
                    if (ps + i == 1)
                        continue;
                    int t = last - cs - (pr + i);
                    if (r * 2 <= t && r * 5 >= t)
                    {
                        done = true;
                        int q = t / r;
                        int m = t % r;
                        int ptr = pr + i + 1;
                        for (int j = 0; j < r; j++)
                        {
                            int lim = j < m ? q + 1 : q;
                            for (int k = 0; k < lim; k++)
                            {
                                a[ptr - 1] = v + j + 1;
                                ptr++;
                            }
                        }
                        for (int j = 1; j <= i; j++)
                            a[pr + j - 1] = v;
                        for (int j = 1; j < cs; j++)
                            a[last - j - 1] = a[last - 1];
                    }
                }
            }

            cs = ps;
            last = pr;
        }

        Write(lv);
        WriteArray(a);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;

    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("cycle.in");
        //writer = new StreamWriter("cycle.out");
#endif
        try
        {
            //            var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //            thread.Start();
            //            thread.Join();
            new Solver().Solve();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            Console.WriteLine(ex);
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write

    private static Queue<string> currentLineTokens = new Queue<string>();

    private static string[] ReadAndSplitLine()
    {
        return reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public static string ReadToken()
    {
        while (currentLineTokens.Count == 0)
            currentLineTokens = new Queue<string>(ReadAndSplitLine());
        return currentLineTokens.Dequeue();
    }

    public static int ReadInt()
    {
        return int.Parse(ReadToken());
    }

    public static long ReadLong()
    {
        return long.Parse(ReadToken());
    }

    public static double ReadDouble()
    {
        return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
    }

    public static int[] ReadIntArray()
    {
        return ReadAndSplitLine().Select(int.Parse).ToArray();
    }

    public static long[] ReadLongArray()
    {
        return ReadAndSplitLine().Select(long.Parse).ToArray();
    }

    public static double[] ReadDoubleArray()
    {
        return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
    }

    public static int[][] ReadIntMatrix(int numberOfRows)
    {
        int[][] matrix = new int[numberOfRows][];
        for (int i = 0; i < numberOfRows; i++)
            matrix[i] = ReadIntArray();
        return matrix;
    }

    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows);
        int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = new int[numberOfRows];
            for (int j = 0; j < numberOfRows; j++)
                ret[i][j] = matrix[j][i];
        }
        return ret;
    }

    public static string[] ReadLines(int quantity)
    {
        string[] lines = new string[quantity];
        for (int i = 0; i < quantity; i++)
            lines[i] = reader.ReadLine().Trim();
        return lines;
    }

    public static void WriteArray<T>(IEnumerable<T> array)
    {
        writer.WriteLine(string.Join(" ", array));
    }

    public static void Write(params object[] array)
    {
        WriteArray(array);
    }

    public static void WriteLines<T>(IEnumerable<T> array)
    {
        foreach (var a in array)
            writer.WriteLine(a);
    }

    class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }

    #endregion
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

// (づ°ω°)づﾐ★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public void Solve()
    {
        int n = ReadInt();
        var a = ReadIntArray();

        var dp = new Tuple<int, int>[6,n + 1];
        dp[5, 0] = Tuple.Create(0, 0);

        int last = 0, lv = 0;
        for (int i = 1; i <= n; i++)
            if (a[i - 1] > 0)
            {
                int d = i - last;
                for (int j = 1; j < 6; j++)
                    if (dp[j, last] != null)
                        for (int k = 1; k < 6; k++)
                        {
                            bool ok = false;
                            if (a[i - 1] == lv)
                            {
                                if (k - j == d)
                                {
                                    ok = true;
                                }
                            }
                            else if (a[i - 1] == lv + 1)
                            {
                                int b = i - k;
                                if (b - last + j > 5 || b < last || j == 1 && b == last)
                                    continue;
                                ok = true;
                            }
                            else
                            {
                                int r = a[i - 1] - lv - 1;
                                for (int x = 0; j + x < 6; x++)
                                {
                                    if (j + x < 2)
                                        continue;
                                    int t = i - k - (last + x);
                                    if (r * 2 <= t && r * 5 >= t)
                                        ok = true;
                                }
                            }
                            if (ok)
                                dp[k, i] = Tuple.Create(j, last);

                        }

                last = i;
                lv = a[i - 1];
            }

        bool f = dp[2, last] != null || dp[3, last] != null || dp[3, last] != null;
        if (dp[1, last] != null && last != n)
            f = true;
        if (dp[5, last] != null && last != n - 1)
            f = true;
        if (!f)
        {
            Write(-1);
            return;
        }

        int cs = 0;
        for (int i = 1; i < 6; i++)
        {
            if (dp[i, last] != null)
            {
                if (i == 1 && last == n || i == 5 && last == n - 1)
                    continue;
                cs = Math.Max(i, cs);
            }
        }

        if (last == n - 1)
            a[n - 1] = lv;
        else
        {
            int p = last + 1;
            int h = cs;
            for (; p <= n; p++)
            {
                if (h > 1 && p < n)
                {
                    lv++;
                    h = 1;
                }
                a[p - 1] = lv;
                h++;
            }
        }

        while (last > 0)
        {
            int ps = dp[cs, last].Item1;
            int pr = dp[cs, last].Item2;
            int v = pr == 0 ? 0 : a[pr - 1];

            if (v == a[last - 1])
            {
                for (int i = pr; i <= last; i++)
                    a[i - 1] = v;
            }
            else if (v == a[last - 1] - 1)
            {
                for (int i = 1; i < cs; i++)
                    a[last - 1 - i] = v + 1;
                for (int i = last - cs; i > pr; i--)
                    a[i - 1] = v;
            }
            else
            {
                bool done = false;
                int r = a[last - 1] - v - 1;
                for (int i = 0; !done && ps + i < 6; i++)
                {
                    if (ps + i == 1)
                        continue;
                    int t = last - cs - (pr + i);
                    if (r * 2 <= t && r * 5 >= t)
                    {
                        done = true;
                        int q = t / r;
                        int m = t % r;
                        int ptr = pr + i + 1;
                        for (int j = 0; j < r; j++)
                        {
                            int lim = j < m ? q + 1 : q;
                            for (int k = 0; k < lim; k++)
                            {
                                a[ptr - 1] = v + j + 1;
                                ptr++;
                            }
                        }
                        for (int j = 1; j <= i; j++)
                            a[pr + j - 1] = v;
                        for (int j = 1; j < cs; j++)
                            a[last - j - 1] = a[last - 1];
                    }
                }
            }

            cs = ps;
            last = pr;
        }

        Write(lv);
        WriteArray(a);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;

    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("cycle.in");
        //writer = new StreamWriter("cycle.out");
#endif
        try
        {
            //            var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //            thread.Start();
            //            thread.Join();
            new Solver().Solve();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            Console.WriteLine(ex);
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write

    private static Queue<string> currentLineTokens = new Queue<string>();

    private static string[] ReadAndSplitLine()
    {
        return reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
    }

    public static string ReadToken()
    {
        while (currentLineTokens.Count == 0)
            currentLineTokens = new Queue<string>(ReadAndSplitLine());
        return currentLineTokens.Dequeue();
    }

    public static int ReadInt()
    {
        return int.Parse(ReadToken());
    }

    public static long ReadLong()
    {
        return long.Parse(ReadToken());
    }

    public static double ReadDouble()
    {
        return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
    }

    public static int[] ReadIntArray()
    {
        return ReadAndSplitLine().Select(int.Parse).ToArray();
    }

    public static long[] ReadLongArray()
    {
        return ReadAndSplitLine().Select(long.Parse).ToArray();
    }

    public static double[] ReadDoubleArray()
    {
        return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
    }

    public static int[][] ReadIntMatrix(int numberOfRows)
    {
        int[][] matrix = new int[numberOfRows][];
        for (int i = 0; i < numberOfRows; i++)
            matrix[i] = ReadIntArray();
        return matrix;
    }

    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows);
        int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = new int[numberOfRows];
            for (int j = 0; j < numberOfRows; j++)
                ret[i][j] = matrix[j][i];
        }
        return ret;
    }

    public static string[] ReadLines(int quantity)
    {
        string[] lines = new string[quantity];
        for (int i = 0; i < quantity; i++)
            lines[i] = reader.ReadLine().Trim();
        return lines;
    }

    public static void WriteArray<T>(IEnumerable<T> array)
    {
        writer.WriteLine(string.Join(" ", array));
    }

    public static void Write(params object[] array)
    {
        WriteArray(array);
    }

    public static void WriteLines<T>(IEnumerable<T> array)
    {
        foreach (var a in array)
            writer.WriteLine(a);
    }

    class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }

    #endregion
}


/* Author : RISHAV GOYAL */

#include <bits/stdc++.h>

using namespace std;

#define LL long long int
#define UL unsigned long long int

#define imax INT_MAX
#define imin INT_MIN
#define LLmax LLONG_MAX
#define LLmin LLONG_MIN

#define FOR(i,a,b) for(int i= (int)a; i<= (int)b; i++)
#define rep(i,n) for(int i= int(1); i<= (int)n; i++)
#define FORd(i,a,b,d) for(int i=(int)a; i<= (int)b; i+=(int)d)

#define pr() printf("Reached here 1...\n");
#define pr1() printf("Reached here 2...\n");

#define CLR(a) memset(a,0,sizeof(a));
#define SET(a) memset(a,-1,sizeof(a));

#define str strlen
#define pb(x) push_back(x)
#define mp make_pair
#define ii pair<int,int>
#define ll pair<LL,LL>
#define F first
#define S second
#define gcd(a,b) __gcd(a,b)

#define mod int(1e9 +7)
#define MAX int(2e5 + 10)

#define si(a) scanf("%d",&a);

#define VI vector<int>
#define VL vector<LL>
#define VS vector<string>
#define VC vector<char>

LL powmod(LL a,int b,int n){LL rm=1;while (b){if (b % 2) { rm = (rm * a) % n; }a = (a * a) % n;b /= 2;}return rm;}

int lower[MAX], upper[MAX];
int n, a[MAX], le[MAX], ri[MAX];

int main() {

    cin >> n;
    rep(i,n) si(a[i])

    lower[0] = upper[0] = 0;
    for(int i = 1; i <= n; i++) {
        le[i] = n + 1;
        ri[i] = -1;
    }

    int maxNum = -1;

    for(int i = 1; i <= n; i++)
        if (a[i]) {
            if (a[i] < 1 || a[i] > n) {
                printf("-1\n");
                return 0;
            }
            int x = a[i];
            le[x] = min(le[x], i);
            ri[x] = max(ri[x], i);
            maxNum = max(maxNum, a[i]);
        }

    if (maxNum == -1) {
        if (n < 2) {
            printf("-1\n");
        } else {
            printf("%d\n", n / 2);
            for(int i = 1; i <= n / 2; i++) {
                a[i * 2 - 1] = a[i * 2] = i;
            }
            if (n % 2 == 1)
                a[n] = a[n - 1];
            for(int i = 1; i <= n; i++) {
                printf("%d%c", a[i], i == n ? '\n' : ' ');
            }
        }

        return 0;
    }

    for(int i = 1; i <= n; i++) {
        if(ri[i] == -1) {
            lower[i] = lower[i - 1] + 2;
            upper[i] = upper[i - 1] + 5;
        }
        else {
            int tmp = ri[i] - le[i] + 1;
            if(tmp > 5){
                printf("-1\n");
                return 0;
            }

            if(lower[i - 1] >= le[i] || ri[i] - upper[i - 1] > 5) {
                printf("-1\n");
                return 0;
            }

            lower[i] = max(ri[i], lower[i - 1] + 2);
            upper[i] = min(le[i] + 4, upper[i - 1] + 5);
        }
    }

    int ans = 0;

    for(int i=maxNum;i<=n;i++) {
        if (lower[i] <= n && upper[i] >= n)
            ans = i;
    }

    if(ans == 0){
        printf("-1\n");
        return 0;
    }

    printf("%d\n", ans);

    int nn = n;

    for(int i = ans; i >= 1; i--) {
        for(int j = 2; j <= 5; j++) {
            if (ri[i] != -1 && le[i] < nn - j + 1)
                continue;
            int tmp = nn - j;
            bool flag = true;
            if (lower[i - 1] <= tmp && tmp <= upper[i - 1]) {
                for(int k = tmp + 1; k <= nn; k++)
                    if (a[k] && a[k] != i)
                        flag = false;
                if (!flag)
                    continue;
                for(int k = tmp + 1; k <= nn; k++)
                    a[k] = i;
                nn = tmp;
                break;
            }
        }
    }

    for(int i = 1; i <= n; i++) {
        printf("%d%c", a[i], i == n ? '\n' : ' ');
    }

    return 0;
}


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


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


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


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


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