#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
char s[maxn];
struct Matrix
{
    int a[5][5];
    Matrix(){ memset(a,0x3f,sizeof(a)); }
    Matrix operator * (const Matrix &rhs) const{
        Matrix res;
        for(int i = 0; i < 5; i++){
            for(int j = 0; j < 5; j++){
                for(int k = 0; k < 5; k++){
                    res.a[i][j] = min(res.a[i][j],a[i][k]+rhs.a[k][j]);
                }
            }
        }
        return res;
    }
};
struct node{ int l, r; Matrix mat; } tree[maxn*8];
void build(int k,int l,int r)
{
    tree[k].l = l; tree[k].r = r;
    if(l == r){
        for(int i = 0; i < 5; i++) tree[k].mat.a[i][i] = 0;
        if(s[l] == '2') tree[k].mat.a[0][0] = 1, tree[k].mat.a[0][1] = 0;
        if(s[l] == '0') tree[k].mat.a[1][1] = 1, tree[k].mat.a[1][2] = 0;
        if(s[l] == '1') tree[k].mat.a[2][2] = 1, tree[k].mat.a[2][3] = 0;
        if(s[l] == '7') tree[k].mat.a[3][3] = 1, tree[k].mat.a[3][4] = 0;
        if(s[l] == '6') tree[k].mat.a[3][3] = 1, tree[k].mat.a[4][4] = 1;
        return ;
    }
    int mid = (l+r)>>1;
    build(k<<1,l,mid); build(k<<1|1,mid+1,r);
    tree[k].mat = tree[k<<1].mat*tree[k<<1|1].mat;
}
Matrix query(int k,int l,int r)
{
    if(tree[k].l == l && tree[k].r == r){
        return tree[k].mat;
    }
    int mid = (tree[k].l+tree[k].r)>>1;
    if(mid >= r) return query(k<<1,l,r);
    else if(mid < l) return query(k<<1|1,l,r);
    return query(k<<1,l,mid)*query(k<<1|1,mid+1,r);
}
int main()
{
    int n, m, x, y;
    scanf("%d%d%s",&n,&m,s+1);
    build(1,1,n);
    while(m--){
        scanf("%d%d",&x,&y);
        Matrix ans = query(1,x,y);
        printf("%d\n",(ans.a[0][4] == 0x3f3f3f3f ? -1 : ans.a[0][4]));
    }
    return 0;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

const int lim = 1 << 18;
struct Mat {
	int cost[5][5];
};
Mat seg[2*lim+1];
int lenString, nbQuery;
char tr[5];

Mat compose(Mat a, Mat b) {
	Mat c;
	for (int i = 0; i < 5; ++i) {
		for (int j = 0; j < 5; ++j) {
			c.cost[i][j] = lim;
		}
	}
	for (int beg = 0; beg < 5; ++beg) {
		for (int mid = beg; mid < 5; ++mid) {
			for (int lst = mid; lst < 5; ++lst) {
				int sub = a.cost[beg][mid] + b.cost[mid][lst];
				c.cost[beg][lst] = min(c.cost[beg][lst], sub);
			}
		}
	}
	return c;
}

int getAns(int lo, int ri) {
	lo += lim;
	ri += lim+1;
	vector<int> leftGr, rightGr;
	while (lo < ri) {
		if (lo & 1) leftGr.push_back(lo++);
		if (ri & 1) rightGr.push_back(--ri);
		lo /= 2; ri /= 2;
	}
	reverse(rightGr.begin(), rightGr.end());
	Mat res;
	if (!leftGr.empty())
		res = seg[leftGr.front()];
	else
		res = seg[rightGr.front()];
	for (int pos : leftGr) {
		if (pos == leftGr.front()) continue;
		res = compose(res, seg[pos]);
	}
	for (int pos : rightGr) {
		if (leftGr.empty() && pos == rightGr.front()) continue;
		res = compose(res, seg[pos]);
	}

	if (res.cost[0][4] <= lenString)
		return res.cost[0][4];
	else
		return -1;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> lenString >> nbQuery;
	string orig;
	cin >> orig;
	tr[0] = '2';
	tr[1] = '0';
	tr[2] = '1';
	tr[3] = '7';
	tr[4] = 'x';

	for (int iChar = 0; iChar < lenString; ++iChar) {
		char c = orig[iChar];
		for (int prev = 0; prev < 5; ++prev) {
			for (int after = 0; after < 5; ++after) {
				int &res = seg[iChar+lim].cost[prev][after];
				res = lim;
				if (prev == after) {
					if (c == '6') {
						res = (prev >= 3);
					} else {
						res = (c == tr[prev]);
					}
				} else if (prev+1 == after && c == tr[prev]) {
					res = 0;
				}
			}
		}
	}

	for (int iElem = lim-1; iElem >= 1; --iElem) {
		seg[iElem] = compose(seg[2*iElem], seg[2*iElem+1]);
	}

	for (int iQuery = 0; iQuery < nbQuery; ++iQuery) {
		int lo, ri;
		cin >> lo >> ri;
		--lo; --ri;
		cout << getAns(lo, ri) << "\n";
	}

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;

const int NMAX = 200010, INF = 0x3f3f3f3f;
int n, q;
char str[NMAX];
struct Matrix{
	int n, m;
	int mat[5][5];
	void init(){
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				if(i == j) mat[i][j] = 0;
				else mat[i][j] = INF;
			}
		}
	}

	void init2(){
		memset(mat, 0x3f, sizeof(mat));
	}
};

Matrix operator * (Matrix a, Matrix b){
	//广义矩阵乘法
	Matrix c;
	c.n = a.n; c.m = b.m;
	c.init2();
	for(int k = 0; k < a.m; k++){
		for(int i = 0; i < c.n; i++){
			for(int j = 0; j < c.m; j++){
				c.mat[i][j] = min(c.mat[i][j], a.mat[i][k] + b.mat[k][j]);
			}
		}
	}
	return c;
}

struct Node{
	Matrix val;
	Node(){
		val.n = val.m = 5;
		val.init();
	}
};

Node segtree[NMAX << 2];

void update(Node &a, Node &b, Node &c){
	a.val = b.val * c.val;
}

Node query(int l, int r, int p = 1, int L = 1, int R = n){
	if(l <= L && R <= r) return segtree[p];
	int mid = (L + R) / 2;
	if(r <= mid) return query(l, r, p * 2, L, mid);
	if(l > mid) return query(l, r, p * 2 + 1, mid + 1, R);
	Node ret1, ret2, ret3;
	ret2 = query(l, r, p * 2, L, mid);
	ret3 = query(l, r, p * 2 + 1, mid + 1, R);
	update(ret1, ret3, ret2);
	return ret1;
}

void revise(int pos, Matrix val, int p = 1, int L = 1, int R = n){
	if(L == R){
		segtree[p].val = val;
		return;
	}
	int mid = (L + R) / 2;
	if(pos <= mid) revise(pos, val, p * 2, L, mid);
	if(pos > mid) revise(pos, val, p * 2 + 1, mid + 1, R);
	update(segtree[p], segtree[p * 2 + 1], segtree[p * 2]);
}

Matrix trans[6];

int main(){
	//freopen("test.in", "r", stdin);
	scanf("%d%d", &n, &q);
	scanf("%s", str + 1);
	for(int i = 0; i < 6; i++){
		trans[i].n = trans[i].m = 5;
		trans[i].init();
	}
	trans[0].mat[0][0] = 1;
	trans[0].mat[1][0] = 0;
	trans[1].mat[1][1] = 1;
	trans[1].mat[2][1] = 0;
	trans[2].mat[2][2] = 1;
	trans[2].mat[3][2] = 0;
	trans[3].mat[3][3] = 1;
	trans[3].mat[4][3] = 0;
	trans[4].mat[3][3] = 1;
	trans[4].mat[4][4] = 1;
	for(int i = 1; i <= n; i++){
		if(i == 4)
			printf("");
		if(str[i] == '2') revise(i, trans[0]);
		else if(str[i] == '0') revise(i, trans[1]);
		else if(str[i] == '1') revise(i, trans[2]);
		else if(str[i] == '7') revise(i, trans[3]);
		else if(str[i] == '6') revise(i, trans[4]);
		else revise(i, trans[5]);
	}
	Matrix tmp;
	tmp.n = 5, tmp.m = 1;
	tmp.init();
	while(q--){
		int l, r;
		scanf("%d%d", &l, &r);
		auto ret = query(l, r).val;
		int ans = (ret * tmp).mat[4][0];
		if(ans == INF) ans = -1;
		printf("%d\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int inf=0x3f3f3f3f;
const int N=2e5+5;
struct matrix
{
    int mat[5][5];
    void init()
    {
        for(int i=0;i<5;i++)
            for(int j=0;j<5;j++)
                mat[i][j]=inf;
    }
    matrix operator +(const matrix &b)const
    {
        matrix res;
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<5;j++)
            {
                res.mat[i][j]=inf;
                for(int k=0;k<5;k++)
                    res.mat[i][j]=min(res.mat[i][j],mat[i][k]+b.mat[k][j]);
            }
        }
        return res;
    }
}tree[N<<2];
char ss[N];
void build(int l,int r,int rt)
{
    if(l==r)
    {//构造转移矩阵：
        tree[rt].init();
        for(int i=0;i<5;i++) tree[rt].mat[i][i]=0;
        if(ss[l]=='2') tree[rt].mat[0][0]=1,tree[rt].mat[0][1]=0;
        if(ss[l]=='0') tree[rt].mat[1][1]=1,tree[rt].mat[1][2]=0;
        if(ss[l]=='1') tree[rt].mat[2][2]=1,tree[rt].mat[2][3]=0;
        if(ss[l]=='7') tree[rt].mat[3][3]=1,tree[rt].mat[3][4]=0;
        if(ss[l]=='6') tree[rt].mat[4][4]=1,tree[rt].mat[3][3]=1;
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
matrix query(int l,int r,int L,int R,int rt)
{
    if(L<=l&&r<=R)
        return tree[rt];
    int mid=(l+r)>>1;
    matrix res;
    res.init();
    for(int i=0;i<5;i++) res.mat[i][i]=0;
    if(L<=mid) res=res+query(l,mid,L,R,rt<<1);
    if(R>mid) res=res+query(mid+1,r,L,R,rt<<1|1);
    return res;
}
int main()
{
    int n,q,l,r;
    scanf("%d%d",&n,&q);
    scanf("%s",ss+1);
    build(1,n,1);
    while(q--)
    {
        scanf("%d%d",&l,&r);
        matrix ans=query(1,n,l,r,1);
        printf("%d\n",ans.mat[0][4]==inf?-1:ans.mat[0][4]);
    }
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<db> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 1000000007;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };

char s[200005];
struct T{
	int a[5][5];
	T(){
		MEM_1(a);
		fup(i,0,4,1)a[i][i]=0;
	}
}tree[524288];
T merge(T t1,T t2){
	T res;
	fup(i,0,4,1)res.a[i][i]=-1;
	fup(i,0,4,1){
		fup(j,i,4,1){
			if(t1.a[i][j]==-1)continue;
			fup(k,j,4,1){
				if(t2.a[j][k]==-1)continue;
				if(res.a[i][k]==-1)res.a[i][k]=t1.a[i][j]+t2.a[j][k];
				else res.a[i][k]=min(res.a[i][k],t1.a[i][j]+t2.a[j][k]);
			}
		}
	}
	return res;
}
void init(int node,int S,int E){
	if(S==E){
		if(s[S]=='2'){
			tree[node].a[0][0]=1;
			tree[node].a[0][1]=0;
		}
		if(s[S]=='0'){
			tree[node].a[1][1]=1;
			tree[node].a[1][2]=0;
		}
		if(s[S]=='1'){
			tree[node].a[2][2]=1;
			tree[node].a[2][3]=0;
		}
		if(s[S]=='6'){
			tree[node].a[3][3]=1;
			tree[node].a[4][4]=1;
		}
		if(s[S]=='7'){
			tree[node].a[3][3]=1;
			tree[node].a[3][4]=0;
		}
		return;
	}
	init(node*2,S,(S+E)/2);init(node*2+1,(S+E)/2+1,E);
	tree[node]=merge(tree[node*2],tree[node*2+1]);
}
T find(int node,int S,int E,int i,int j){
	T tmp;
	tmp.a[4][0]=-2;
	if (i > E || j < S)return tmp;
	if (i <= S && j >= E)return tree[node];
	T L=find(node*2,S,(S+E)/2,i,j),R=find(node*2+1,(S+E)/2+1,E,i,j);
	if(L.a[4][0]==-2)return R;
	if(R.a[4][0]==-2)return L;
	return merge(L,R);
}
int main(){
	int n,q;
	scanf("%d%d",&n,&q);
	scanf("%s",s+1);
	init(1,1,n);
	while(q--){
		int x,y;
		scanf("%d%d",&x,&y);
		T res=find(1,1,n,x,y);
		printf("%d\n",res.a[0][4]);
	}
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef vector<int> vi;
// END NO SAD

typedef long long ll;
typedef pair<int, int> pii;

struct node {
  int trans[5][5];
};

node init(char ch) {
  node ret;
  memset(ret.trans, 1, sizeof(ret.trans));
  for(int i = 0; i < 5; i++) ret.trans[i][i] = 0;
  if(ch == '2') {
    ret.trans[0][0] = 1;
    ret.trans[0][1] = 0;
  }
  if(ch == '0') {
    ret.trans[1][1] = 1;
    ret.trans[1][2] = 0;
  }
  if(ch == '1') {
    ret.trans[2][2] = 1;
    ret.trans[2][3] = 0;
  }
  if(ch == '7') { // good
    ret.trans[3][3] = 1;
    ret.trans[3][4] = 0;
  }
  if(ch == '6') { // bad
    ret.trans[3][3] = 1;
    ret.trans[4][4] = 1;
  }
  return ret;
}

node merge(const node& a, const node& b) {
  node ret;
  memset(ret.trans, 1, sizeof(ret.trans));
  for(int i = 0; i < 5; i++) for(int j = i; j < 5; j++) for(int k = i; k <= j; k++) {
    ret.trans[i][j] = min(ret.trans[i][j], a.trans[i][k] + b.trans[k][j]);
  }
  return ret;
}

const int RAGETREE_SZ = 1 << 18;
node ragetree[2 * RAGETREE_SZ];
node qry(int lhs, int rhs) {
  lhs += RAGETREE_SZ;
  rhs += RAGETREE_SZ;
  vector<int> lv, rv;
  while(lhs <= rhs) {
    if(lhs == rhs) {
      lv.push_back(lhs);
      break;
    }
    if(lhs%2) lv.push_back(lhs++);
    if(rhs%2==0) rv.push_back(rhs--);
    lhs /= 2;
    rhs /= 2;
  }
  reverse(all(rv));
  assert(sz(lv) + sz(rv));
  node init;
  if(sz(rv)) {
    init = ragetree[rv.back()]; rv.pop_back();
  }
  else {
    assert(sz(lv));
    init = ragetree[lv.back()]; lv.pop_back();
  }
  while(sz(rv)) {
    init = merge(ragetree[rv.back()], init); rv.pop_back();
  }
  while(sz(lv)) {
    init = merge(ragetree[lv.back()], init); lv.pop_back();
  }
  return init;
}

void solve() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  for(int i = 0; i < sz(s); i++) {
    ragetree[i + RAGETREE_SZ] = init(s[i]);
  }
  for(int i = RAGETREE_SZ-1; i > 0; i--) {
    ragetree[i] = merge(ragetree[2*i], ragetree[2*i+1]);
  }
  while(q--) {
    int lhs, rhs;
    cin >> lhs >> rhs;
    node ret = qry(--lhs, --rhs);
    if(ret.trans[0][4] > n) cout << "-1\n";
    else cout << ret.trans[0][4] << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef vector<int> vi;
// END NO SAD

typedef long long ll;
typedef pair<int, int> pii;

struct node {
  int trans[5][5];
};

node init(char ch) {
  node ret;
  memset(ret.trans, 1, sizeof(ret.trans));
  for(int i = 0; i < 5; i++) ret.trans[i][i] = 0;
  if(ch == '2') {
    ret.trans[0][0] = 1;
    ret.trans[0][1] = 0;
  }
  if(ch == '0') {
    ret.trans[1][1] = 1;
    ret.trans[1][2] = 0;
  }
  if(ch == '1') {
    ret.trans[2][2] = 1;
    ret.trans[2][3] = 0;
  }
  if(ch == '7') { // good
    ret.trans[3][3] = 1;
    ret.trans[3][4] = 0;
  }
  if(ch == '6') { // bad
    ret.trans[3][3] = 1;
    ret.trans[4][4] = 1;
  }
  return ret;
}

node merge(const node& a, const node& b) {
  node ret;
  memset(ret.trans, 1, sizeof(ret.trans));
  for(int i = 0; i < 5; i++) for(int j = i; j < 5; j++) for(int k = i; k <= j; k++) {
    ret.trans[i][j] = min(ret.trans[i][j], a.trans[i][k] + b.trans[k][j]);
  }
  return ret;
}

const int RAGETREE_SZ = 1 << 17;
node ragetree[2 * RAGETREE_SZ];
node qry(int lhs, int rhs) {
  lhs += RAGETREE_SZ;
  rhs += RAGETREE_SZ;
  vector<int> lv, rv;
  while(lhs <= rhs) {
    if(lhs == rhs) {
      lv.push_back(lhs);
      break;
    }
    if(lhs%2) lv.push_back(lhs++);
    if(rhs%2==0) rv.push_back(rhs--);
    lhs /= 2;
    rhs /= 2;
  }
  reverse(all(rv));
  assert(sz(lv) + sz(rv));
  node init;
  if(sz(rv)) {
    init = ragetree[rv.back()]; rv.pop_back();
  }
  else {
    assert(sz(lv));
    init = ragetree[lv.back()]; lv.pop_back();
  }
  while(sz(rv)) {
    init = merge(ragetree[rv.back()], init); rv.pop_back();
  }
  while(sz(lv)) {
    init = merge(ragetree[lv.back()], init); lv.pop_back();
  }
  return init;
}

void solve() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  for(int i = 0; i < sz(s); i++) {
    ragetree[i + RAGETREE_SZ] = init(s[i]);
  }
  for(int i = RAGETREE_SZ-1; i > 0; i--) {
    ragetree[i] = merge(ragetree[2*i], ragetree[2*i+1]);
  }
  while(q--) {
    int lhs, rhs;
    cin >> lhs >> rhs;
    node ret = qry(--lhs, --rhs);
    if(ret.trans[0][4] > n) cout << "-1\n";
    else cout << ret.trans[0][4] << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
}


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

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double
#define ll long long

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define data asd09123jdf02i3h

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)

const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;

char s[maxn];
int n,q;

#define ls p<<1
#define rs p<<1|1
#define mid ((l+r)>>1)

struct segment_tree {
    struct node {
        int a[5][5];

        void init(int x) {
            for(int i=0;i<5;i++)
                for(int j=0;j<5;j++)
                    a[i][j]=i==j?0:inf;
            if(x==2) a[0][1]=0,a[0][0]=1;
            else if(x==0) a[1][2]=0,a[1][1]=1;
            else if(x==1) a[2][3]=0,a[2][2]=1;
            else if(x==7) a[3][4]=0,a[3][3]=1;
            else if(x==6) a[3][3]=1,a[4][4]=1;
        }

        node operator + (const node &r) const {
            node v;memset(v.a,63,sizeof v.a);
            for(int i=0;i<=4;i++)
                for(int j=i;j<=4;j++)
                    for(int k=j;k<=4;k++)
                        v.a[i][k]=min(v.a[i][k],a[i][j]+r.a[j][k]);
            return v;
        }
    }t[maxn<<2];

    void modify(int p,int l,int r,int x,int v) {
        if(l==r) return t[p].init(v),void();
        if(x<=mid) modify(ls,l,mid,x,v);
        else modify(rs,mid+1,r,x,v);
        t[p]=t[ls]+t[rs];
    }

    node query(int p,int l,int r,int x,int y) {
        if(x<=l&&r<=y) return t[p];
        if(y<=mid) return query(ls,l,mid,x,y);
        if(x>mid) return query(rs,mid+1,r,x,y);
        return query(ls,l,mid,x,y)+query(rs,mid+1,r,x,y);
    }
}T;

int main() {
    read(n),read(q);scanf("%s",s+1);
    for(int i=1;i<=n;i++) T.modify(1,1,n,i,s[i]-'0');
    while(q--) {
        int l,r;read(l),read(r);
        int x=T.query(1,1,n,l,r).a[0][4];
        write(x>n?-1:x);
    }
    return 0;
}


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

#define MAXN 200007

int n , q ;
string a ;

int pat[ 4 ] = { 2 , 0 , 1 , 7 } ;

struct node {
    int f[ 5 ][ 5 ] ;
    node ( ) {
        for ( int i = 0 ; i < 5 ; ++ i ) {
            for ( int j = 0 ; j < 5 ; ++ j ) {
                if ( i != j ) {
                    f[ i ][ j ] = MAXN ;
                }
                else {
                    f[ i ][ j ] = 0 ;
                }
            }
        }
    }
};

node unite ( node p1 , node p2 ) {
    node ret ;
    for ( int i = 0 ; i < 5 ; ++ i ) {
        for ( int j = i ; j < 5 ; ++ j ) {
            ret.f[ i ][ j ] = MAXN ;
            for ( int t = i ; t <= j ; ++ t ) {
                ret.f[ i ][ j ] = min ( ret.f[ i ][ j ] , p1.f[ i ][ t ] + p2.f[ t ][ j ] ) ;
            }
        }
    }
    return ret ;
}

class Tree {
public :
    node tr[ 4 * MAXN ] ;
    void init ( int where , int IL , int IR ) {
        if ( IL == IR ) {
            for ( int i = 0 ; i < 5 ; ++ i ) {
                for ( int j = 0 ; j < 5 ; ++ j ) {
                    tr[ where ].f[ i ][ j ] = MAXN ;
                    if ( i == j ) {
                        if ( i >= 3 && a[ IL - 1 ] == '6' ) {
                            tr[ where ].f[ i ][ j ] = 1 ;
                        }
                        else {
                            if ( a[ IL - 1 ] - '0' == pat[ i ] ) {
                                tr[ where ].f[ i ][ j ] = 1 ;
                            }
                            else {
                                tr[ where ].f[ i ][ j ] = 0 ;
                            }
                        }
                    }
                    if ( j == i + 1 && a[ IL - 1 ] - '0' == pat[ i ] ) {
                        tr[ where ].f[ i ][ j ] = 0 ;
                    }
                }
            }
            return ;
        }
        int mid = ( IL + IR ) / 2 ;
        init ( 2 * where , IL , mid ) ;
        init ( 2 * where + 1 , mid + 1 , IR ) ;
        tr[ where ] = unite ( tr[ 2 * where ] , tr[ 2 * where + 1 ] ) ;
    }
    node query ( int where , int IL , int IR , int CURL , int CURR ) {
        if ( CURL > CURR ) { return node ( ) ; }
        if ( CURR < IL || IR < CURL ) { return node ( ) ; }
        if ( CURL <= IL && IR <= CURR ) { return tr[ where ] ; }
        int mid = ( IL + IR ) / 2 ;
        return unite ( query ( 2 * where , IL , mid , CURL , CURR ) , query ( 2 * where + 1 , mid + 1 , IR , CURL , CURR ) ) ;
    }
};
Tree w ;

void input ( ) {
    cin >> n >> q ;
    cin >> a ;
    w.init ( 1 , 1 , n ) ;
}

void solve ( ) {
    while ( q -- ) {
        int l , r ;
        cin >> l >> r ;
        node ret = w.query ( 1 , 1 , n , l , r ) ;
        if ( ret.f[ 0 ][ 4 ] >= MAXN ) { cout << "-1\n" ; }
        else { cout << ret.f[ 0 ][ 4 ] << "\n" ; }
    }
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    input ( ) ;
    solve ( ) ;
    return 0 ;
}


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

const long long llINF = 9223372036854775807;
const int INF = 2147483647;
const int maxn = 2e5 + 7;
const int maxm = 5e4 + 7;
const int ALP = 26;
const long long mod = 998244353;
const double pi = acos(-1.0);
const double eps = 1e-12;

int n,Q;
char s[maxn];

struct node
{
	int l,r;
	int matrix[5][5];
/*
the minimum number of digits to remove so that
if there it was already possible to get prefix of "2017" of length i,
then with this segment of digits it will be possible to get prefix of "2017" of length j
*/
	void init()
	{
		for(int i=0;i<5;i++)
			for(int j=0;j<5;j++)
				matrix[i][j]=maxn;
	}
}seg_tree[maxn<<2];

node operator+(node a,node b)
{
	node res;
	res.init();
	for(int k=0;k<5;k++)
		for(int i=0;i<5;i++)
			for(int j=0;j<5;j++)
				res.matrix[i][j]=min(res.matrix[i][j],a.matrix[i][k]+b.matrix[k][j]);
	res.l=min(a.l,b.l);
	res.r=max(a.r,b.r);
	return res;
}

void push_up(int root)
{
	seg_tree[root]=seg_tree[root<<1]+seg_tree[root<<1|1];
}

void build(int root,int l,int r)
{
	seg_tree[root].l=l;
	seg_tree[root].r=r;
	//cout<<root<<" "<<l<<" "<<r<<endl;
	if(l==r)
	{
		for(int i=0;i<5;i++)
			for(int j=0;j<5;j++)
				seg_tree[root].matrix[i][j]=(i==j)?0:1e9;
		if(s[l]=='2')
		{
			seg_tree[root].matrix[0][0]=1;
			seg_tree[root].matrix[0][1]=0;
		}
		if(s[l]=='0')
		{
			seg_tree[root].matrix[1][1]=1;
			seg_tree[root].matrix[1][2]=0;
		}
		if(s[l]=='1')
		{
			seg_tree[root].matrix[2][3]=0;
			seg_tree[root].matrix[2][2]=1;
		}
        if(s[l]=='7')
		{
			seg_tree[root].matrix[3][3]=1;
			seg_tree[root].matrix[3][4]=0;
        }
		if(s[l]=='6')
		{
			seg_tree[root].matrix[3][3]=1;
			seg_tree[root].matrix[4][4]=1;
		}
		return;
	}
	int mid=(l+r)>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	push_up(root);
	//cout<<root<<" "<<seg_tree[root].l<<" "<<seg_tree[root].r<<endl;
}

node query(int root,int l,int r)
{
	//cout<<seg_tree[root].l<<" "<<seg_tree[root].r<<" "<<l<<" "<<r<<endl;
	//cout<<root<<" "<<seg_tree[root].l<<" "<<seg_tree[root].r<<endl;
	if(l==seg_tree[root].l&&seg_tree[root].r==r)
		return seg_tree[root];
	int mid=(seg_tree[root].l+seg_tree[root].r)>>1;
	if(r<=mid)
		return query(root<<1,l,r);
	if(l>mid)
		return query(root<<1|1,l,r);
	return query(root<<1,l,mid)+query(root<<1|1,mid+1,r);
}

int main()
{
	scanf("%d%d",&n,&Q);
	scanf("%s",s+1);
	build(1,1,n);
 	while(Q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		//cout<<l<<" "<<r<<endl;
		int ans=query(1,l,r).matrix[0][4];
		printf("%d\n",ans>n?-1:ans);
	}
	return 0;
}


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

const long long llINF = 9223372036854775807;
const int INF = 2147483647;
const int maxn = 2e5 + 7;
const int maxm = 5e4 + 7;
const int ALP = 26;
const long long mod = 998244353;
const double pi = acos(-1.0);
const double eps = 1e-12;

int n,Q;
char s[maxn];

struct node
{
	int l,r;
	int matrix[5][5];
/*
the minimum number of digits to remove so that
if there it was already possible to get prefix of "2017" of length i,
then with this segment of digits it will be possible to get prefix of "2017" of length j
*/
	void init()
	{
		for(int i=0;i<5;i++)
			for(int j=0;j<5;j++)
				matrix[i][j]=maxn;
	}
}seg_tree[maxn<<2];

node operator+(node a,node b)
{
	node res;
	res.init();
	for(int i=0;i<5;i++)
		for(int j=0;j<5;j++)
			for(int k=0;k<5;k++)
				res.matrix[i][j]=min(res.matrix[i][j],a.matrix[i][k]+b.matrix[k][j]);
	res.l=min(a.l,b.l);
	res.r=max(a.r,b.r);
	return res;
}

void push_up(int root)
{
	seg_tree[root]=seg_tree[root<<1]+seg_tree[root<<1|1];
}

void build(int root,int l,int r)
{
	seg_tree[root].l=l;
	seg_tree[root].r=r;
	//cout<<root<<" "<<l<<" "<<r<<endl;
	if(l==r)
	{
		for(int i=0;i<5;i++)
			for(int j=0;j<5;j++)
				seg_tree[root].matrix[i][j]=(i==j)?0:1e9;
		if(s[l]=='2')
		{
			seg_tree[root].matrix[0][0]=1;
			seg_tree[root].matrix[0][1]=0;
		}
		if(s[l]=='0')
		{
			seg_tree[root].matrix[1][1]=1;
			seg_tree[root].matrix[1][2]=0;
		}
		if(s[l]=='1')
		{
			seg_tree[root].matrix[2][3]=0;
			seg_tree[root].matrix[2][2]=1;
		}
        if(s[l]=='7')
		{
			seg_tree[root].matrix[3][3]=1;
			seg_tree[root].matrix[3][4]=0;
        }
		if(s[l]=='6')
		{
			seg_tree[root].matrix[3][3]=1;
			seg_tree[root].matrix[4][4]=1;
		}
		return;
	}
	int mid=(l+r)>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	push_up(root);
	//cout<<root<<" "<<seg_tree[root].l<<" "<<seg_tree[root].r<<endl;
}

node query(int root,int l,int r)
{
	//cout<<seg_tree[root].l<<" "<<seg_tree[root].r<<" "<<l<<" "<<r<<endl;
	//cout<<root<<" "<<seg_tree[root].l<<" "<<seg_tree[root].r<<endl;
	if(l==seg_tree[root].l&&seg_tree[root].r==r)
		return seg_tree[root];
	int mid=(seg_tree[root].l+seg_tree[root].r)>>1;
	if(r<=mid)
		return query(root<<1,l,r);
	if(l>mid)
		return query(root<<1|1,l,r);
	return query(root<<1,l,mid)+query(root<<1|1,mid+1,r);
}

int main()
{
	scanf("%d%d",&n,&Q);
	scanf("%s",s+1);
	build(1,1,n);
 	while(Q--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		//cout<<l<<" "<<r<<endl;
		int ans=query(1,l,r).matrix[0][4];
		printf("%d\n",ans>n?-1:ans);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#define kl (k<<1)
#define kr (k<<1|1)
#define M (L+R>>1)
#define lin L,M
#define rin M+1,R
using namespace std;

struct Matrix
{
	int dp[5][5];

	init()
	{
		for(int i=0;i<5;i++)
			for(int j=0;j<5;j++)
				dp[i][j]=-1;
	}

	Matrix operator * (const Matrix &t) const
	{
		Matrix res;
		res.init();
		for(int i=0;i<5;i++)
			for(int j=0;j<5;j++)
				for(int k=0;k<5;k++)
					if(dp[i][k]!=-1&&t.dp[k][j]!=-1)
						if(res.dp[i][j]==-1)
							res.dp[i][j]=dp[i][k]+t.dp[k][j];
						else
							res.dp[i][j]=min(res.dp[i][j],dp[i][k]+t.dp[k][j]);
		return res;
	}
}T[1<<19],tmp;

int n,m,a[200005],l,r;
char s[200005];

void build_tree(int k, int L, int R)
{
	if(L==R)
	{
		T[k].init();
		for(int i=0;i<5;i++)
			T[k].dp[i][i]=0;
		if(a[L]==2)
			T[k].dp[0][1]=0,T[k].dp[0][0]=1;
		else if(a[L]==0)
			T[k].dp[1][2]=0,T[k].dp[1][1]=1;
		else if(a[L]==1)
			T[k].dp[2][3]=0,T[k].dp[2][2]=1;
		else if(a[L]==7)
			T[k].dp[3][4]=0,T[k].dp[3][3]=1;
		else if(a[L]==6)
			T[k].dp[3][3]=T[k].dp[4][4]=1;
		return ;
	}
	build_tree(kl,lin);
	build_tree(kr,rin);
	T[k]=T[kl]*T[kr];
}

Matrix query(int k, int L, int R, int l, int r)
{
	if(l<=L&&R<=r)
		return T[k];
	if(r<=M)
		return query(kl,lin,l,r);
	if(l>M)
		return query(kr,rin,l,r);
	return query(kl,lin,l,r)*query(kr,rin,l,r);
}

int main()
{
	scanf("%d%d%s",&n,&m,s+1);
	for(int i=1;i<=n;i++)
		a[i]=s[i]-'0';
	build_tree(1,1,n);
	while(m--)
	{
		scanf("%d%d",&l,&r);
		tmp=query(1,1,n,l,r);
		printf("%d\n",tmp.dp[0][4]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1 << 19 | 7;
const int inf = 0x3f3f3f3f;
#define rep(i, n) for(int i=0; i<n; i++)
struct Node {
  int e[5][5];
  void merge(const Node &l, const Node &r){
    rep(i, 5) rep(j, 5) e[i][j]=inf;
    rep(i, 5) rep(k, 5)
      rep(j, 5) e[i][j]=min(e[i][j],l.e[i][k]+r.e[k][j]);
  }
  void init(int ch){
    rep(i, 5) rep(j, 5) e[i][j]=inf;
    rep(i, 5) e[i][i]=0;
    if(ch==2) e[0][1]=0,e[0][0]=1;
    else if(ch==0)e[1][2]=0,e[1][1]=1;
    else if(ch==1)e[2][3]=0,e[2][2]=1;
    else if(ch==7)e[3][4]=0,e[3][3]=1;
    else if(ch==6)e[3][3]=1,e[4][4]=1;
  }
};

Node tr[N];
char s[N];
#define ls o<<1
#define rs o<<1|1

void build(int o,int l,int r){
  if(l==r)return tr[o].init(s[l]-'0');
  int mid=(l+r)>>1;
  build(ls,l,mid),build(rs,mid+1,r);
  tr[o].merge(tr[ls],tr[rs]);
}

Node query(int o, int l, int r, int ql, int qr){
  if(ql<=l&&r<=qr) return tr[o];
  int mid=(l+r)>>1;
  if(mid>=qr) return query(ls,l,mid,ql,qr);
  if(mid<ql) return query(rs,mid+1,r,ql,qr);
  Node ret; ret.merge(query(ls,l,mid,ql,qr),query(rs,mid+1,r,ql,qr));
  return ret;
}

int main() {
#ifdef local
  freopen("in.txt","r",stdin);
#endif
  int n, q; scanf("%d%d",&n,&q);
  scanf("%s",s+1);
  build(1,1,n);
  while(q--){
    int l, r; scanf("%d%d", &l, &r);
    Node ret = query(1,1,n,l,r);
    printf("%d\n",ret.e[0][4]==inf?-1:ret.e[0][4]);
  }
  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=200001;
const int INF=1061109567;
int n,q;
char s[N];
struct Matrix
{
	int mat[5][5];
    Matrix()
    {
        memset(mat,63,sizeof(mat));
        return;
    }
    Matrix operator=(const char &c)
    {
        for(int i=0;i<5;i++)
			for(int j=0;j<5;j++)
				mat[i][j]=(i==j)?0:INF;
		if(c=='2') mat[0][1]=0,mat[0][0]=1;
		if(c=='0') mat[1][2]=0,mat[1][1]=1;
		if(c=='1') mat[2][3]=0,mat[2][2]=1;
		if(c=='7') mat[3][4]=0,mat[3][3]=1;
		if(c=='6') mat[3][3]=1,mat[4][4]=1;
        return *this;
    }
	Matrix operator*(const Matrix &b)const
    {
		Matrix res;
		for(int i=0;i<5;i++)
			for(int j=0;j<5;j++)
				for(int k=0;k<5;k++)
					res.mat[i][j]=min(res.mat[i][j],mat[i][k]+b.mat[k][j]);
		return res;
	}
};
struct Node
{
    int l,r;
    Matrix sum;
}tree[N<<2];
void push_up(int i)
{
    tree[i].sum=tree[i*2].sum*tree[i*2+1].sum;
    return;
}
void build(int i,int l,int r)
{
    tree[i].l=l,tree[i].r=r;
	if(l==r)
    {
        tree[i].sum=s[l];
		return;
	}
	int mid=(l+r)/2;
	build(i*2,l,mid);
    build(i*2+1,mid+1,r);
    push_up(i);
    return;
}
Matrix query(int i,int L,int R)
{
	if(L<=tree[i].l&&tree[i].r<=R) return tree[i].sum;
    if(R<=tree[i*2].r) return query(i*2,L,R);
    else if(L>=tree[i*2+1].l) return query(i*2+1,L,R);
    else return query(i*2,L,R)*query(i*2+1,L,R);
}
int main()
{
    scanf("%d%d%s",&n,&q,s+1);
	build(1,1,n);
	while(q--)
    {
		int l,r;
		scanf("%d%d",&l,&r);
		int ans=query(1,l,r).mat[0][4];
		if(ans>=INF) ans=-1;
		printf("%d\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,q;string s;
const int INF = 0x3f;
struct Martix{
    int a[5][5];
    Martix(){
        memset(a,INF, sizeof(a));
        a[0][0]=a[1][1]=a[2][2]=a[3][3]=a[4][4]=0;
    }
    Martix operator +(const Martix b)const {
        Martix res;
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                res.a[i][j]=INF;
                for(int k=0;k<5;k++){
                    res.a[i][j]=min(res.a[i][j],a[i][k]+b.a[k][j]);
                }
            }
        }
        return res;
    }
};
Martix sum[1048577];
void build(int k,int l,int r){
    if(l==r){
        if(s[l]=='2'){
            sum[k].a[0][0]=1;
            sum[k].a[0][1]=0;
        }else if(s[l]=='0'){
            sum[k].a[1][1]=1;
            sum[k].a[1][2]=0;
        }else if(s[l]=='1'){
            sum[k].a[2][2]=1;
            sum[k].a[2][3]=0;
        }else if(s[l]=='7'){
            sum[k].a[3][3]=1;
            sum[k].a[3][4]=0;
        }else if(s[l]=='6'){
            sum[k].a[3][3]=1;
            sum[k].a[4][4]=1;
        }
        return;
    }
    int mid=l+r>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
    sum[k]=sum[k<<1]+sum[k<<1|1];
}
Martix query(int k,int l,int r,int lef,int rig){
    if(l>=lef&&rig>=r){
        return sum[k];
    }
    int mid = l+r>>1;
    if(rig<=mid)return query(k<<1,l,mid,lef,rig);
    if(lef>mid)return query(k<<1|1,mid+1,r,lef,rig);
    return query(k<<1,l,mid,lef,rig)+query(k<<1|1,mid+1,r,lef,rig);
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>q;
    cin>>s;s="*"+s;
    build(1,1,n);
    int l,r;
    while (q--){
        cin>>l>>r;
        int ans = query(1,1,n,l,r).a[0][4];
        if(ans>=INF){
            cout<<-1<<endl;
        }else{
            cout<<ans<<endl;
        }
    }
}


n = int(input())
q = list(map(int,input().split()))
p = []
control = []

for i in range(0,n+1):
  control.append(0)

sum1 = 0
sum2 = 0
sum_p = (1+n)*n//2

for i in range(0,n-1):
  sum1 += q[i]
  sum2 += sum1

f = sum_p-sum2

if (f%n == 0 and f//n > 0 and f//n <= n):
  control[f//n] = 1
else:
  control[1] = -1

p.append(f//n)

for i in range(1,n):
  p.append(p[i-1]+q[i-1])
  if (p[i] >= 1 and p[i] <= n):
    control[p[i]] += 1

res = 0

for i in range(1,n+1):
  if (control[i] == 1):
    res += 1

if (res != n):
  print(-1)
else:
  for i in range(0,n):
    print(p[i],end=" ")


n = int(input())
q = list(map(int,input().split()))
p = []
control = []

for i in range(0,n+1):
  control.append(0)

sum1 = 0
sum2 = 0
sum_p = (1+n)*n//2

for i in range(0,n-1):
  sum1 += q[i]
  sum2 += sum1

f = sum_p-sum2

if (f%n == 0 and f//n > 0):
  control[f//n] = 1
else:
  control[1] = -1

p.append(f//n)

for i in range(1,n):
  p.append(p[i-1]+q[i-1])
  if (p[i] >= 1 and p[i] <= n):
    control[p[i]] += 1

res = 0

for i in range(1,n+1):
  if (control[i] == 1):
    res += 1

if (res != n):
  print(-1)
else:
  for i in range(0,n):
    print(p[i],end=" ")


n = int(input())
l =  list(map(int,input().split()))
d=[]
for i in range(1,n+1):
    d.append(i)
for i in range(1,n+1):
    q=[]
    for s  in range(n):
        q.append(i+sum(l[:s]))
    f=q[::1]
    f.sort()
    if d==f:
        print(q)
        break


n = int(input())
l =  list(map(int,input().split()))
d=[]
c=n*(n+1)//2
for i in range(1,n+1):
    d.append(i)
for i in range(n-1):
    c=c-l[i]*(n-i-1)
c=c//n
q=[]
for i in range(n):
    q.append(c+sum(l[:i]))
f=q[::1]
f.sort()
if f==d:
    print(*q)
else:
    print(-1)


n = int(input())
l =  list(map(int,input().split()))
d=[]
for i in range(1,n+1):
    d.append(i)
for i in range(1,n+1):
    q=[]
    for s  in range(n):
        q.append(i+sum(l[:s]))
    f=q[::1]
    f.sort()
    if d==f:
        print(*q)
        break


n = int(input())
l =  list(map(int,input().split()))
d=[]
k=0
for i in range(1,n+1):
    d.append(i)
if sum(l)>n-1 or sum(l)<1-n:
    print(-1)
else:
    for i in range(1,n+1):
        q=[]
        for s  in range(n):
            q.append(i+sum(l[:s]))
        f=q[::1]
        f.sort()
        if d==f:
            print(*q)
            k=6
            break
    if k==0:
        print(-1)


n = int(input())
l =  list(map(int,input().split()))
d=[]
c=n*(n+1)//2
for i in range(1,n+1):
    d.append(i)
for i in range(n-1):
    c=c-l[i]*(n-i-1)
c=c//n
q=[c]
for i in range(n-1):
    q.append(l[i]+q[i])
f=q[::1]
f.sort()
if f==d:
    print(*q)
else:
    print(-1)


n = int(input())
l =  list(map(int,input().split()))
d=[]
k=0
for i in range(1,n+1):
    d.append(i)
for i in range(1,n+1):
    q=[]
    for s  in range(n):
        q.append(i+sum(l[:s]))
    f=q[::1]
    f.sort()
    if d==f:
        print(*q)
        k=6
        break
if k==0:
    print(-1)


n = int(input())
l =  list(map(int,input().split()))
d=[]
k=0
for i in range(1,n+1):
    d.append(i)
for i in range(1,n+1):
    q=[]
    for s  in range(n):
        q.append(i+sum(l[:s]))
    f=q[::1]
    f.sort()
    if d==f:
        print(q)
        k=6
        break
if k==0:
    print(-1)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0] * n

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'
for i in range(1, n+1):
    if 2 <= min_q_new + max_q_new + 2*i <= 2*n:

        p[0] = i

        for j in range(1, n):
            qqq = set()
            p[j] = q[j-1] + p[j-1]
            if p[j] not in qqq:
                qqq.add(p[j])
            else:
                break
            if p[j] <= 0 or p[j] > n:
                break

        if len(set(p)) == n and max(p) == n and min(p) == 1:
            rez = ' '.join(str(i) for i in p)
            break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
# n = 15_000_000
# q = [1 for i in range(n-1)]
p = [0 for i in range(n)]

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'

for i in range(1, n+1):
    if (1-i <= min_q_new <= n-i) and (1-i <= max_q_new <= n-i):

        p[0] = i

        for j in range(1, n):
            qqq = set()
            p[j] = q[j-1] + p[j-1]
            if p[j] not in qqq:
                qqq.add(p[j])
            else:
                break
            # if p[j] <= 0 or p[j] > n:
            #     break

        if len(set(p)) == n and max(p) == n and min(p) == 1:
            rez = ' '.join(map(str, p))
            break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]

p = [0 for i in range(n)]

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'
if len(set(q_new)) == n:
    pass
else:
    for i in [1-min_q_new, n-max_q_new]:
        if (1-i <= min_q_new <= n-i) and (1-i <= max_q_new <= n-i):

            p[0] = i

            for j in range(1, n):
                p[j] = q[j-1] + p[j-1]

            if len(set(p)) == n and max(p) == n and min(p) == 1:
                rez = ' '.join(map(str, p))
                break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0] * n

rez = '-1'
for i in range(1, n+1):
    p[0] = i

    for j in range(1, n):
        qqq = set()
        p[j] = q[j-1] + p[j-1]
        if p[j] not in qqq:
            qqq.add(p[j])
        else:
            break
        if p[j] <= 0 or p[j] > n:
            break

    if len(set(p)) == n and max(p) == n and min(p) == 1:
        rez = ' '.join(str(i) for i in p)
        break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0] * n

rez = '-1'
for i in range(1, n+1):
    p[0] = i

    for j in range(1, n):
        p[j] = q[j-1] + p[j-1]
        if p[j] <= 0:
            break

    if len(set(p)) == n and max(p) == n:
        rez = ' '.join(str(i) for i in p)
        print(rez)

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0] * n

rez = '-1'
for i in range(1, n+1):
    p[0] = i

    for j in range(1, n):
        qqq = set()
        p[j] = q[j-1] + p[j-1]
        if p[j] not in qqq:
            qqq.add(p[j])
        else:
            break
        if p[j] <= 0 or p[j] > n:
            break
    rez = ' '.join(str(i) for i in p)

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
# n = 15_000_000
# q = [1 for i in range(n-1)]
p = [0 for i in range(n)]

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'
if len(set(q_new)) == n:
    pass
else:
    for i in [1-min_q_new, n-max_q_new]:
        if (1-i <= min_q_new <= n-i) and (1-i <= max_q_new <= n-i):

            p[0] = i

            for j in range(1, n):
                qqq = set()
                p[j] = q[j-1] + p[j-1]
                if p[j] not in qqq:
                    qqq.add(p[j])
                else:
                    break
                # if p[j] <= 0 or p[j] > n:
                #     break

            if len(set(p)) == n and max(p) == n and min(p) == 1:
                rez = ' '.join(map(str, p))
                break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0] * n

rez = '-1'
for i in range(1, n+1):
    p[0] = i

    for j in range(1, n):
        qqq = set()
        p[j] = q[j-1] + p[j-1]
        if p[j] not in qqq:
            qqq.add(p[j])
        else:
            break

    if len(set(p)) == n and max(p) == n and min(p) == 1:
        rez = ' '.join(str(i) for i in p)
        break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0] * n

rez = '-1'
for i in range(1, n+1):
    p[0] = i

    for j in range(1, n):
        qqq = set()
        p[j] = q[j-1] + p[j-1]
        if p[j] not in qqq and p[j] > 0 and p[j] <=n:
            qqq.add(p[j])
        else:
            break

    if len(set(p)) == n and max(p) == n and min(p) == 1:
        rez = ' '.join(str(i) for i in p)
        break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0 for i in range(n)]

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'
if len(set(q_new)) == n:
    pass
else:
    for i in [1-min_q_new, n-max_q_new]:
        if (1-i <= min_q_new <= n-i) and (1-i <= max_q_new <= n-i):

            p[0] = i

            for j in range(1, n):
                p[j] = q[j-1] + p[j-1]
                rez = ' '.join(map(str, p))
                break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
# n = 15_000_000
# q = [1 for i in range(n-1)]
p = [0 for i in range(n)]

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'
if len(set(q_new)) == n:
    pass
else:
    for i in range(1, n+1):
        if (1-i <= min_q_new <= n-i) and (1-i <= max_q_new <= n-i):

            p[0] = i

            for j in range(1, n):
                qqq = set()
                p[j] = q[j-1] + p[j-1]
                if p[j] not in qqq:
                    qqq.add(p[j])
                else:
                    break
                # if p[j] <= 0 or p[j] > n:
                #     break

            if len(set(p)) == n and max(p) == n and min(p) == 1:
                rez = ' '.join(map(str, p))
                break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0 for i in range(n)]

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'

for i in range(1, n+1):
    if (1-i <= min_q_new <= n-i) and (1-i <= max_q_new <= n-i):

        p[0] = i

        for j in range(1, n):
            qqq = set()
            p[j] = q[j-1] + p[j-1]
            if p[j] not in qqq:
                qqq.add(p[j])
            else:
                break
            if p[j] <= 0 or p[j] > n:
                break

        if len(set(p)) == n and max(p) == n and min(p) == 1:
            rez = ' '.join(map(str, p))
            break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0] * n

rez = '-1'
for i in range(1, n+1):
    p[0] = i

    for j in range(1, n):
        p[j] = q[j-1] + p[j-1]
        if p[j] <= 0:
            break

    if len(set(p)) == n and max(p) == n:
        rez = ' '.join(str(i) for i in p)

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0] * n

rez = '-1'
for i in range(1, n+1):
    p[0] = i

    for j in range(1, n):
        p[j] = q[j-1] + p[j-1]
        if p[j] <= 0 or p[j] > n:
            break

    if len(set(p)) == n and max(p) == n and min(p) == 1:
        rez = ' '.join(str(i) for i in p)
        break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0] * n

rez = '-1'
for i in range(1, n+1):
    p[0] = i

    for j in range(1, n):
        p[j] = q[j-1] + p[j-1]
        if p[j] <= 0 or p[j] > n:
            break

    if len(set(p)) == n and max(p) == n:
        rez = ' '.join(str(i) for i in p)
        break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
p = [0] * n

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'
for i in range(1, n+1):
    if (1-i <= min_q_new <= n-i) and (1-i <= max_q_new <= n-i):

        p[0] = i

        for j in range(1, n):
            qqq = set()
            p[j] = q[j-1] + p[j-1]
            if p[j] not in qqq:
                qqq.add(p[j])
            else:
                break
            if p[j] <= 0 or p[j] > n:
                break

        if len(set(p)) == n and max(p) == n and min(p) == 1:
            rez = ' '.join(str(i) for i in p)
            break

print(rez)


# n = int(input())
# q = input()
# q = q.split(' ')
# q = [int(i) for i in q]
n = 15_000_000
q = [1 for i in range(14_999_999)]
p = [0 for i in range(n)]

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'

for i in range(1, n+1):
    if (1-i <= min_q_new <= n-i) and (1-i <= max_q_new <= n-i):

        p[0] = i

        for j in range(1, n):
            qqq = set()
            p[j] = q[j-1] + p[j-1]
            if p[j] not in qqq:
                qqq.add(p[j])
            else:
                break
            if p[j] <= 0 or p[j] > n:
                break

        if len(set(p)) == n and max(p) == n and min(p) == 1:
            rez = ' '.join(map(str, p))
            break

print(rez)


import sys

n = int(input())
q = sys.stdin.readline().split(' ')
q = [int(i) for i in q]
# n = 15_000_000
# q = [1 for i in range(14_999_999)]
p = [0 for i in range(n)]

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'

for i in range(1, n+1):
    if (1-i <= min_q_new <= n-i) and (1-i <= max_q_new <= n-i):

        p[0] = i

        for j in range(1, n):
            qqq = set()
            p[j] = q[j-1] + p[j-1]
            if p[j] not in qqq:
                qqq.add(p[j])
            else:
                break
            if p[j] <= 0 or p[j] > n:
                break

        if len(set(p)) == n and max(p) == n and min(p) == 1:
            rez = ' '.join(map(str, p))
            break

print(rez)


n = int(input())
q = input().split(' ')
q = [int(i) for i in q]
# n = 15_000_000
# q = [1 for i in range(n-1)]
p = [0 for i in range(n)]

q_new = q[:]
for i in range(1, len(q)):
    q_new[i] = q_new[i-1] + q_new[i]
min_q_new = min(q_new)
max_q_new = max(q_new)

rez = '-1'

for i in [n-max_q_new, ]:
    if (1-i <= min_q_new <= n-i) and (1-i <= max_q_new <= n-i):

        p[0] = i

        for j in range(1, n):
            qqq = set()
            p[j] = q[j-1] + p[j-1]
            if p[j] not in qqq:
                qqq.add(p[j])
            else:
                break
            # if p[j] <= 0 or p[j] > n:
            #     break

        if len(set(p)) == n and max(p) == n and min(p) == 1:
            rez = ' '.join(map(str, p))
            break

print(rez)


n=int(input())
q=list(map(int,input().split()))
s=0
ms=0
for x in q:
    s+=x
    if s<ms:ms=s
a=[0]*n
a[0]=1-ms
for i in range(1,n):
    a[i]=a[i-1]+q[i-1]
    if a[i]<1 or a[i]>n:
        print(-1)
        exit()
print(*a)


n=int(input())
q=list(map(int,input().split()))
s=0
ms=0
for x in q:
    s+=x
    if s<ms:ms=s
a=[0]*n
a[0]=1-ms
for i in range(1,n):
    a[i]=a[i-1]+q[i-1]
    if a[i]<1 or a[i]>n:
        print(-1)
        exit()
if len(set(a))==n:
    print(*a)
else:
    print(-1)


n=int(input())
q=list(map(int,input().split()))
s=0
ms=0
for x in q:
    s+=x
    if s<ms:ms=s
a=[0]*n
a[0]=1-ms
if a[0]<1 or a[0]>n:
    print(-1)
    exit()
for i in range(1,n):
    a[i]=a[i-1]+q[i-1]
    if a[i]<1 or a[i]>n:
        print(-1)
        exit()
if len(set(a))==n:
    print(*a)
else:
    print(-1)


n=int(input())
a=list(map(int, input().split()))
x=0
y=0
for i in range(n-1):
    y+=a[i]
    x+=y
x1=(-x+((n+1)*n)//2)//n
if (-x+((n+1)*n)//2)%n!=0:
    print(-1)
else:
    b=[]
    b.append(x1)
    for i in range(1, n):
        b.append(b[i-1]+a[i-1])
    if (max(b)==n) and (min(b)==1):
        print(*b)
    else:
        print(-1)


n=int(input())
a=list(map(int, input().split()))
x=0
y=0
for i in range(n-1):
    y+=a[i]
    x+=y
x1=(-x+((n+1)*n)//2)//n
if (-x+((n+1)*n)//2)%n!=0:
    print(-1)
else:
    b=[]
    b.append(x1)
    for i in range(1, n):
        b.append(b[i-1]+a[i-1])
    print(*b)


n=int(input())
a=list(map(int, input().split()))
x=0
y=0
c=[]
for i in range(n-1):
    y+=a[i]
    x+=y
x1=(-x+((n+1)*n)//2)//n
h=0
if (-x+((n+1)*n)//2)%n!=0:
    print(-1)
else:
    b=[]
    b.append(x1)
    c.append(x1)
    for i in range(1, n):
        b.append(b[i-1]+a[i-1])
        c.append(b[i-1]+a[i-1])
    sorted(c)
    for i in range(1, n):
        if c[i]-c[i-1]==1:
            h+=1
    if h==n-1:
        print(*b)
    else:
        print(-1)


n=int(input())
a=list(map(int, input().split()))
x=0
y=0
c=[]
for i in range(n-1):
    y+=a[i]
    x+=y
x1=(-x+((n+1)*n)//2)//n
h=0
if (-x+((n+1)*n)//2)%n!=0:
    print(-1)
else:
    b=[]
    b.append(x1)
    c.append(x1)
    for i in range(1, n):
        b.append(b[i-1]+a[i-1])
        c.append(b[i-1]+a[i-1])
    c=sorted(c)
    for i in range(1, n):
        if c[i]-c[i-1]==1:
            h+=1
    if h==n-1:
        print(*b)
    else:
        print(-1)


n = int(input())
q = [int(i) for i in input().split()]
r = [q[0]]
for i in range(1,n-1):
	r.append(r[i-1]+q[i])
# print(r)
k = min(r)
if k < 0:
	for i in range(len(r)):
		r[i] = r[i] -  k + 1
	if max([-k+1]+r) == n:
			print(*[-k+1]+r)
	else:
		print(-1)
else:
	if k == 1:
		for i in range(len(r)):
			r[i] += 1
		r = [1]+r
		print(*r)
	else:
		print(-1)


n = int(input())
q = [int(i) for i in input().split()]
r = [q[0]]
for i in range(1,n-1):
	r.append(r[i-1]+q[i])
k = min(r)
for i in range(len(r)):
	r[i] = r[i] -  k + 1
r = [-k+1]+r
if set(range(1,n+1))==set(r):
	print(*r)
else:
	print(-1)


n = int(input())
q = [int(i) for i in input().split()]
r = [q[0]]
for i in range(1,n-1):
	r.append(r[i-1]+q[i])
k = min(r)
# print(k)
for i in range(len(r)):
	r[i] = r[i] -  k + 1 if k < 0 else r[i]+1
r = [-k+1]+r if k < 0 else [k]+r
if set(range(1,n+1))==set(r):
	print(*r)
else:
	print(-1)
# print(r)


n = int(input())
q = [int(i) for i in input().split()]
r = [q[0]]
for i in range(1,n-1):
	r.append(r[i-1]+q[i])
# print(r)
k = min(r)
# print(k)
for i in range(len(r)):
	r[i] = r[i] -  k + 1 if k <= 0 else r[i]+1
# print(*([-k+1]+r) if max([-k+1]+r) <= n else -1)
if max([-k+1]+r) == n:
	if k <= 0:
		print(*[-k+1]+r)
		# print('a')
	else:
		print(*[k]+r)
else:
	print(-1)


n = int(input())
q = [int(i) for i in input().split()]
r = [q[0]]
for i in range(1,n-1):
	r.append(r[i-1]+q[i])
# print(r)
k = min(r)
if k < 0:
	for i in range(len(r)):
		r[i] = r[i] -  k + 1
	r = [-k+1]+r
	if max(r) == n and len(set(r)) == len(r):
			print(*r)
	else:
		print(-1)
else:
	if k == 1:
		for i in range(len(r)):
			r[i] += 1
		r = [1]+r
		if max(r) == n and len(set(r)) == len(r):
			print(*r)
	else:
		print(-1)


n = int(input())
q = [int(i) for i in input().split()]
r = [q[0]]
for i in range(1,n-1):
	r.append(r[i-1]+q[i])
# print(r)
k = min(r)
# print(k)
for i in range(len(r)):
	r[i] = r[i] -  k + 1 if k <= 0 else r[i]+1
# print(*([-k+1]+r) if max([-k+1]+r) <= n else -1)
if max([-k+1]+r) <= n:
	if k <= 0:
		print(*[-k+1]+r)
		# print('a')
	else:
		print(*[k]+r)
else:
	print(-1)


n = int(input())
q = [int(i) for i in input().split()]
r = [q[0]]
for i in range(1,n-1):
	r.append(r[i-1]+q[i])
k = min(r)
for i in range(len(r)):
	r[i] = r[i] -  k + 1
r = [-k+1]+r
if set(range(1,n+1))==set(r):
	print(*r)


n = int(input())
l = map(int, input().split())
s = [0]

for new in l:
    new += s[-1]
    if new in s:
        print(-1)
        break
    s.append(new)

if max(s) - min(s) + 1 == n:
    print(' '.join([i + min(s) + 1 for i in s]))
else:
    print(-1)

# ordered
# inclusion


n = int(input())
l = map(int, input().split())
s = [0]

for new in l:
    new += s[-1]
    if new in s:
        print(-1)
        break
    s.append(new)

if max(s) - min(s) + 1 == n:
    print(' '.join([str(i - min(s) + 1) for i in s]))
else:
    print(-1)

# ordered
# inclusion


n = int(input())
l = map(int, input().split())
# s = [0]
# s_set = {0}

# for new in l:
#     if new > n-1:
#         print(-1)
#         break
#     new += s[-1]
#     if new in s_set:
#         print(-1)
#         break
#     s.append(new)
# else:
#     if max(s) - min(s) + 1 == n:
#         print(' '.join([str(i - min(s) + 1) for i in s]))
#     else:
#         print(-1)

# for new in l:
#     s.append(new + s[-1])

# s = [(i - min(s) + 1) for i in s]
# for i in range(1, n+1):
#     if not i in s:
#         print(-1)
#         break
# else:
#     print(' '.join([str(i) for i in s]))

# for new in l:
#     s.append(new + s[-1])

# s_set = {(i - min(s) + 1) for i in s}
# for i in range(1, n+1):
#     if not i in s_set:
#         print(-1)
#         break
# else:
#     m = min(s)
#     print(' '.join([str(i - m + 1) for i in s]))

a = [0]
for i in l:
    a.append(a[-1] + i)

b = sorted(a[::])
for i in range(n-1):
    if b[i+1] - b[i] != 1:
        print(-1)
        break
else:
    m = 1 - b[0]
    for i in range(n):
        print(m + a[i], end=' ')

# ordered
# inclusion


n = int(input())
l = map(int, input().split())
s = [0]

for new in l:
    new += s[-1]
    if new in s:
        print(-1)
        break
    s.append(new)
else:
    if max(s) - min(s) + 1 == n:
        print(' '.join([str(i - min(s) + 1) for i in s]))
    else:
        print(-1)

# for new in l:
#     s.append(new + s[-1])

# s = [(i - min(s) + 1) for i in s]
# for i in range(1, n+1):
#     if not i in s:
#         print(-1)
#         break
# else:
#     print(' '.join([str(i) for i in s]))

# ordered
# inclusion


n = int(input())
l = map(int, input().split())
s = [0]

# for new in l:
#     new += s[-1]
#     if new in s:
#         print(-1)
#         break
#     s.append(new)

# if max(s) - min(s) + 1 == n:
#     print(' '.join([str(i - min(s) + 1) for i in s]))
# else:
#     print(-1)

for new in l:
    s.append(new + s[-1])

s = {(i - min(s) + 1) for i in s}
for i in range(1, n+1):
    if not i in s:
        print(-1)
        break
else:
    print(' '.join(s))

# ordered
# inclusion


n = int(input())
l = map(int, input().split())
s = [0]
# s_set = {0}

# for new in l:
#     if new > n-1:
#         print(-1)
#         break
#     new += s[-1]
#     if new in s_set:
#         print(-1)
#         break
#     s.append(new)
# else:
#     if max(s) - min(s) + 1 == n:
#         print(' '.join([str(i - min(s) + 1) for i in s]))
#     else:
#         print(-1)

# for new in l:
#     s.append(new + s[-1])

# s = [(i - min(s) + 1) for i in s]
# for i in range(1, n+1):
#     if not i in s:
#         print(-1)
#         break
# else:
#     print(' '.join([str(i) for i in s]))

for new in l:
    s.append(new + s[-1])

s_set = {(i - min(s) + 1) for i in s}
for i in range(1, n+1):
    if not i in s_set:
        print(-1)
        break
else:
    m = min(s)
    print(' '.join([str(i - m + 1) for i in s]))

# ordered
# inclusion


n = int(input())
l = map(int, input().split())
s = [0]

for new in l:
    if new > n-1:
        print(-1)
        break
    new += s[-1]
    if new in s:
        print(-1)
        break
    s.append(new)
else:
    if max(s) - min(s) + 1 == n:
        print(' '.join([str(i - min(s) + 1) for i in s]))
    else:
        print(-1)

# for new in l:
#     s.append(new + s[-1])

# s = [(i - min(s) + 1) for i in s]
# for i in range(1, n+1):
#     if not i in s:
#         print(-1)
#         break
# else:
#     print(' '.join([str(i) for i in s]))

# ordered
# inclusion


n = int(input())
l = map(int, input().split())
s = [0]

for new in l:
    new += s[-1]
    if new in s:
        print(-1)
        break
    s.append(new)

if max(s) - min(s) + 1 == n:
    print(' '.join([str(i - min(s) + 1) for i in s]))
else:
    print(-1)

# for new in l:
#     s.append(new + s[-1])

# s = [(i - min(s) + 1) for i in s]
# for i in range(1, n+1):
#     if not i in s:
#         print(-1)
#         break
# else:
#     print(' '.join([str(i) for i in s]))

# ordered
# inclusion


n = int(input())
l = map(int, input().split())
s = [0]

# for new in l:
#     new += s[-1]
#     if new in s:
#         print(-1)
#         break
#     s.append(new)

# if max(s) - min(s) + 1 == n:
#     print(' '.join([str(i - min(s) + 1) for i in s]))
# else:
#     print(-1)

for new in l:
    s.append(new + s[-1])

s = [(i - min(s) + 1) for i in s]
for i in range(1, n+1):
    if not i in s:
        print(-1)
        break
else:
    print(' '.join([str(i) for i in s]))

# ordered
# inclusion


n=int(input())
q=list(map(int,input().split()))
pos=[]
neg=[]
#print(q)
k=flag=0
for i in q:
    if i==0:
        flag=1
        break
    if i<0:
        neg.append(i)
    else:
        pos.append(i)
    k+=1
#print(sum(pos),sum(neg))
if flag==1 or (sum(pos)-sum(neg)!=n and sum(pos)-sum(neg)!=n-1 and sum(pos)-sum(neg)!=n+1 ):
    #print("YASH")
    print(-1)
else:
    ans=[0]*n
    req=[]
    for i in range(1,n+1):
        req.append(i)

    if len(neg)==0:
        ans[0]=1
        for i in range(0,n-1):
           ans[i+1]=ans[i]+q[i]
        #print(ans)
        if req==ans:
            print(*req)
        else:
            print(-1)
    elif len(pos)==0:
        ans[0]=n
        for i in range(0,n-1):
            ans[i+1]=ans[i]+q[i]
        #print(ans)
        ans1=sorted(ans)
        if req==ans1:
            print(*ans)
        else:
            print(-1)
    else:
        arr=[0,0]
        arr[1]=q[0]
        for i in range(1,len(q)):
            arr.append(arr[-1]+q[i])
        #print(arr)
        a=arr.index(min(arr))
        #print(a)
        if q[a]<0:
            a=a+1
        #else:
        ans[a]=1
        #print(ans)
        for i in range(a,n-1):
            #if q[i]<0:
            ans[i+1]=ans[i]+q[i]

        #print(ans)
        i=a-1
        while i>=0:
            ans[i]=ans[i+1]-q[i]
            i-=1
       # print(ans)
        ans1=sorted(ans)
        if ans1==req:
            print(*ans)
        else:
            print(-1)


n=int(input())
q=list(map(int,input().split()))
pos=[]
neg=[]
#print(q)
k=flag=0
for i in q:
    if i==0:
        flag=1
        break
    if i<0:
        neg.append(i)
    else:
        pos.append(i)
    k+=1
#print(sum(pos),sum(neg))
if flag==1 or (sum(pos)-sum(neg)!=n and sum(pos)-sum(neg)!=n-1):
    #print("YASH")
    print(-1)
else:
    ans=[0]*n
    req=[]
    for i in range(1,n+1):
        req.append(i)

    if len(neg)==0:
        ans[0]=1
        for i in range(0,n-1):
           ans[i+1]=ans[i]+q[i]
        #print(ans)
        if req==ans:
            print(*req)
        else:
            print(-1)
    elif len(pos)==0:
        ans[0]=n
        for i in range(0,n-1):
            ans[i+1]=ans[i]+q[i]
        #print(ans)
        ans1=sorted(ans)
        if req==ans1:
            print(*ans)
        else:
            print(-1)
    else:
        arr=[0]
        arr[0]=q[0]
        for i in range(1,len(q)):
            arr.append(arr[-1]+q[i])
        #print(arr)
        a=arr.index(min(arr))
        #print(a)
        if q[a]<0:
            a=a+1
        #else:
        ans[a]=1
       # print(ans)
        for i in range(a,n-1):
            #if q[i]<0:
            ans[i+1]=ans[i]+q[i]

        #print(ans)
        i=a-1
        while i>=0:
            ans[i]=ans[i+1]-q[i]
            i-=1
        #print(ans)
        ans1=sorted(ans)
        if ans1==req:
            print(*ans)
        else:
            print(-1)


n=int(input())
q=list(map(int,input().split()))
pos=[]
neg=[]
#print(q)
k=flag=0
for i in q:
    if i==0:
        flag=1
        break
    if i<0:
        neg.append(i)
    else:
        pos.append(i)
    k+=1
#print(sum(pos),sum(neg))
if flag==1:
    #print("YASH")
    print(-1)
else:
    ans=[0]*n
    req=[]
    for i in range(1,n+1):
        req.append(i)

    if len(neg)==0:
        ans[0]=1
        for i in range(0,n-1):
           ans[i+1]=ans[i]+q[i]
        #print(ans)
        if req==ans:
            print(*req)
        else:
            print(-1)
    elif len(pos)==0:
        ans[0]=n
        for i in range(0,n-1):
            ans[i+1]=ans[i]+q[i]
        #print(ans)
        ans1=sorted(ans)
        if req==ans1:
            print(*ans)
        else:
            print(-1)
    else:
        arr=[0,0]
        arr[1]=q[0]
        for i in range(1,len(q)):
            arr.append(arr[-1]+q[i])
        #print(arr)
        a=arr.index(min(arr))
        #print(a)
        if q[a]<0:
            a=a+1
        #else:
        ans[a]=1
        #print(ans)
        for i in range(a,n-1):
            #if q[i]<0:
            ans[i+1]=ans[i]+q[i]

        #print(ans)
        i=a-1
        while i>=0:
            ans[i]=ans[i+1]-q[i]
            i-=1
       # print(ans)
        ans1=sorted(ans)
        if ans1==req:
            print(*ans)
        else:
            print(-1)


n=int(input())
q=list(map(int,input().split()))
pos=[]
neg=[]
#print(q)
k=flag=0
for i in q:
    if i==0:
        flag=1
        break
    if i<0:
        neg.append(i)
    else:
        pos.append(i)
    k+=1
#print(sum(pos),sum(neg))
if flag==1:
    #print("YASH")
    print(-1)
else:
    ans=[0]*n
    req=[]
    for i in range(1,n+1):
        req.append(i)

    if len(neg)==0:
        ans[0]=1
        for i in range(0,n-1):
           ans[i+1]=ans[i]+q[i]
        #print(ans)
        if req==ans:
            print(*req)
        else:
            print(-1)
    elif len(pos)==0:
        ans[0]=n
        for i in range(0,n-1):
            ans[i+1]=ans[i]+q[i]
        #print(ans)
        ans1=sorted(ans)
        if req==ans1:
            print(*ans)
        else:
            print(-1)
    else:
        arr=[0,0]
        arr[1]=q[0]
        for i in range(1,n-1):
            arr.append(arr[-1]+q[i])

        #print(arr)
        a=arr.index(min(arr))-1
        #print(a)
        if q[a]<0:
            a=a+1
        #else:
        ans[a]=1
        #print(ans)
        for i in range(a,n-1):
            #if q[i]<0:
            ans[i+1]=ans[i]+q[i]

        #print(ans)
        i=a-1
        while i>=0:
            ans[i]=ans[i+1]-q[i]
            i-=1
       # print(ans)
        ans1=sorted(ans)
        if ans1==req:
            print(*ans)
        else:
            print(-1)


n=int(input())
q=list(map(int,input().split()))
pos=[]
neg=[]
#print(q)
k=flag=0
for i in q:
    if i==0:
        flag=1
        break
    if i<0:
        neg.append(i)
    else:
        pos.append(i)
    k+=1
#print(sum(pos),sum(neg))
if flag==1 or (sum(pos)-sum(neg)!=n and sum(pos)-sum(neg)!=n-1):
    #print("YASH")
    print(-1)
else:
    ans=[0]*n
    req=[]
    for i in range(1,n+1):
        req.append(i)

    if len(neg)==0:
        ans[0]=1
        for i in range(0,n-1):
           ans[i+1]=ans[i]+q[i]
        #print(ans)
        if req==ans:
            print(*req)
        else:
            print(-1)
    elif len(pos)==0:
        ans[0]=n
        for i in range(0,n-1):
            ans[i+1]=ans[i]+q[i]
        #print(ans)
        ans1=sorted(ans)
        if req==ans1:
            print(*ans)
        else:
            print(-1)
    else:
        arr=[0,0]
        arr[1]=q[0]
        for i in range(1,len(q)):
            arr.append(arr[-1]+q[i])
        #print(arr)
        a=arr.index(min(arr))
        #print(a)
        if q[a]<0:
            a=a+1
        #else:
        ans[a]=1
        #print(ans)
        for i in range(a,n-1):
            #if q[i]<0:
            ans[i+1]=ans[i]+q[i]

        #print(ans)
        i=a-1
        while i>=0:
            ans[i]=ans[i+1]-q[i]
            i-=1
       # print(ans)
        ans1=sorted(ans)
        if ans1==req:
            print(*ans)
        else:
            print(-1)


n=int(input())
q=list(map(int,input().split()))
pos=[]
neg=[]
#print(q)
k=flag=0
for i in q:
    if i==0:
        flag=1
        break
    if i<0:
        neg.append(i)
    else:
        pos.append(i)
    k+=1
#print(sum(pos),sum(neg))
if flag==1:
    #print("YASH")
    print(-1)
else:
    ans=[0]*n
    req=[]
    for i in range(1,n+1):
        req.append(i)

    if len(neg)==0:
        ans[0]=1
        for i in range(0,n-1):
           ans[i+1]=ans[i]+q[i]
        #print(ans)
        if req==ans:
            print(*req)
        else:
            print(-1)
    elif len(pos)==0:
        ans[0]=n
        for i in range(0,n-1):
            ans[i+1]=ans[i]+q[i]
        #print(ans)
        ans1=sorted(ans)
        if req==ans1:
            print(*ans)
        else:
            print(-1)
    else:
        arr=[0,0]
        arr[1]=q[0]
        for i in range(1,len(q)):
            arr.append(arr[-1]+q[i])

        if min(arr)<=-n or max(arr)>=n:
            print(-1)
        else:
            #print(arr)
            a=arr.index(min(arr))
            #print(a)
            if q[a]<0:
                a=a+1
            #else:
            ans[a]=1
            #print(ans)
            for i in range(a,n-1):
                #if q[i]<0:
                ans[i+1]=ans[i]+q[i]

            #print(ans)
            i=a-1
            while i>=0:
                ans[i]=ans[i+1]-q[i]
                i-=1
           # print(ans)
            ans1=sorted(ans)
            if ans1==req:
                print(*ans)
            else:
                print(-1)


n=int(input())
q=list(map(int,input().split()))
pos=[]
neg=[]
#print(q)
k=flag=0
for i in q:
    if i==0:
        flag=1
        break
    if i<0:
        neg.append(i)
    else:
        pos.append(i)
    k+=1
#print(sum(pos),sum(neg))
if flag==1 or (sum(pos)-sum(neg)!=n and sum(pos)-sum(neg)!=n-1):
    #print("YASH")
    print(-1)
else:
    ans=[0]*n
    req=[]
    for i in range(1,n+1):
        req.append(i)

    if len(neg)==0:
        ans[0]=1
        for i in range(0,n-1):
           ans[i+1]=ans[i]+q[i]
        #print(ans)
        if req==ans:
            print(*req)
        else:
            print(-1)
    elif len(pos)==0:
        ans[0]=n
        for i in range(0,n-1):
            ans[i+1]=ans[i]+q[i]
        #print(ans)
        ans1=sorted(ans)
        if req==ans1:
            print(ans)
        else:
            print(-1)
    else:
        arr=[0]
        arr[0]=q[0]
        for i in range(1,len(q)):
            arr.append(arr[-1]+q[i])
        #print(arr)
        a=arr.index(min(arr))
        #print(a)
        if q[a]<0:
            a=a+1
        #else:
        ans[a]=1
       # print(ans)
        for i in range(a,n-1):
            #if q[i]<0:
            ans[i+1]=ans[i]+q[i]

        #print(ans)
        i=a-1
        while i>=0:
            ans[i]=ans[i+1]-q[i]
            i-=1
        #print(ans)
        ans1=sorted(ans)
        if ans1==req:
            print(*ans)
        else:
            print(-1)


# pega os inputs
n = int(input())
d = input().split()
# "map" o q, transformando os indíces em int
for i in range(n-1):
    d[i] = int(d[i])
# abre a lista da permutação, com todos os índices em 0 (sem abrir não tava rolando)
p = []
for i in range(n):
    p.append(0)
# constrói a permutação
for i in range(n - 1):
    p[i + 1] = d[i] + p[i]
for i in range(n):
    p[i] = p[i] - min(p) + 1
# verifica repeteco e max
if len(set(p)) != n or max(p) > n:
    print(-1)
# tudo certo, printa a permutação
else:
    print(p)


n = int(input())
q = input().split()
for i in range(n-1):
    q[i] = int(q[i])
p = [0 for i in range(n)]
for i in range(n - 1):
    p[i + 1] = q[i] + p[i]
mn = min(p)
for i in range(n):
    p[i] = p[i] - mn + 1
if len(set(p)) != n or max(p) > n:
    print(-1)
else:
    print(*p)


n = int(input())
q = input().split()
for i in range(n-1):
    q[i] = int(q[i])
p = [0 for i in range(n)]
for i in range(n - 1):
    p[i + 1] = q[i] + p[i]
mn = min(p)
for i in range(n):
    p[i] = p[i] - mn + 1
if len(set(p)) != n or max(p) > n:
    print(-1)
else:
    print(p)


n=int(input())
q=list(map(int,input().split()))
p=[0]
for i in range(n-1):
    p.append(p[i]+q[i])
s=sum(p)
#print(s)
x=(n+1)//2-s//n
p=[i+x for i in p]
if(sum(p)==n*(n+1)//2):
    print(*p)
else:
    print(-1)


n=int(input())
q=list(map(int,input().split()))
p=[0]
for i in range(n-1):
    p.append(p[i]+q[i])
s=sum(p)
#print(s)
#1 3 -3
k=n*(n+1)/2
r=list(range(1,n+1))
#print(*r)
#print(k,s)
x=(n+1)//2-s//n
p=[i+x for i in p]
if(sorted(p)==r):
    print(*p)
else:
    print(-1)


n=int(input())
q=list(map(int,input().split()))
p=[0]
for i in range(n-1):
    p.append(p[i]+q[i])
s=sum(p)
#print(s)
x=(n+1)//2-s//n
p=[i+x for i in p]
print(*p)


n=int(input())
q=list(map(int,input().split()))
p=[0]
for i in range(n-1):
    p.append(p[i]+q[i])
s=sum(p)
#print(s)
#1 3 -3
k=n*(n+1)/2
r=range(1,n+1)
#print(*r)
#print(k,s)
x=(n+1)//2-s//n
p=[i+x for i in p]
if(sorted(p)==r):
    print(*p)
else:
    print(-1)


def Check_List(l) -> bool:
    l.sort()
    for i in range (0,len(l)-1):
        if (l[i+1]-1!=l[i]):
            return False
    return True

def Find_Min(l) ->int:
    m = l[0]
    for i in range (0,len(l)):
        if (l[i]<m):
            m=l[i]
    return m

n = int(input())
arr = list(map(int,input().split()))
l = [0]
for i in range(1,n):
    l.append(l[i-1]+arr[i-1])
k = Find_Min(l)
for i in range (0,len(l)):
    l[i]+= 1 - k
ans = []
for i in range(0,len(l)):
    ans.append(l[i])

if (Check_List(l)==True):
    print(ans)
else:
    print(-1)


def Check_List(l) -> bool:
    l.sort()
    for i in range (0,len(l)-1):
        if (l[i+1]-1!=l[i]):
            return False
    return True

def Find_Min(l) ->int:
    m = l[0]
    for i in range (0,len(l)):
        if (l[i]<m):
            m=l[i]
    for i in range (0,len(l)):
        if (m==l[i]):
            m=i
    return m

n = int(input())
arr = list(map(int,input().split()))
l = [0]
for i in range(1,n):
    l.append(l[i-1]+arr[i-1])
while (l[Find_Min(l)]!=1):
    for i in range (0,len(l)):
        l[i]+=1
ans = []
for i in range(0,len(l)):
    ans.append(l[i])
if (Check_List(l)==True):
    print(ans)
else:
    print(-1)


def Check_List(l) -> bool:
    l.sort()
    for i in range (0,len(l)-1):
        if (l[i+1]-1!=l[i]):
            return False
    return True

def Find_Min(l) ->int:
    m = l[0]
    for i in range (0,len(l)):
        if (l[i]<m):
            m=l[i]
    return m

n = int(input())
arr = list(map(int,input().split()))
l = [0]
for i in range(1,n):
    l.append(l[i-1]+arr[i-1])
k = Find_Min(l)
for i in range (0,len(l)):
    l[i]+= 1 - k
ans = []
for i in range(0,len(l)):
    ans.append(l[i])

if (Check_List(l)==True):
    for i in range(0,len(ans)):
        print(ans[i])
else:
    print(-1)


# import time

def solve(n, deltas):
    min_ = 1
    l = [1]
    for ele in deltas:
        new = l[-1] + ele
        min_ = min(min_, new)
        l.append(new)
    l_s = sorted(l)
    for i, ele in enumerate(l_s):
        if i != ele - min_:
            return -1
    return [ele - min_ + 1 for ele in l]

def main():
    n = int(input())
    deltas = list(map(int, input().split()))
    # tick = time.time()
    print(' '.join(map(str, solve(n, deltas))))
    # tock = time.time()
    # print('T:', round(tock - tick, 5))

if __name__ == "__main__":
    main()


# import time

def solve(n, deltas):
    min_ = 1
    l = [1]
    for ele in deltas:
        new = l[-1] + ele
        min_ = min(min_, new)
        l.append(new)
    l_s = sorted(l)
    for i, ele in enumerate(l_s):
        if i != ele - min_:
            return -1
    return [ele - min_ + 1 for ele in l]

def main():
    n = int(input())
    deltas = list(map(int, input().split()))
    # tick = time.time()
    print(solve(n, deltas))
    # tock = time.time()
    # print('T:', round(tock - tick, 5))

if __name__ == "__main__":
    main()


# import time

def solve(n, deltas):
    min_ = 1
    l = [1]
    for ele in deltas:
        new = l[-1] + ele
        min_ = min(min_, new)
        l.append(new)
    l_s = sorted(l)
    for i, ele in enumerate(l_s):
        if i != ele - min_:
            return '-1'
    return ' '.join(map(str, [ele - min_ + 1 for ele in l]))

def main():
    n = int(input())
    deltas = list(map(int, input().split()))
    # tick = time.time()
    print(solve(n, deltas))
    # tock = time.time()
    # print('T:', round(tock - tick, 5))

if __name__ == "__main__":
    main()


n=int(input())
a=list(map(int,input().split()))
ind=-1
for i in range(1,n+1):
    f=[0]*(n+1)
    x=i
    f[x]=1
    flag=1
    c=0
    for j in range(0,n-1):
        x+=a[j]
        if(not (x>=1 and x<=n)):
            flag=0
            break
        if(f[x]==1):
            flag=0
            break
        else:
            f[x]=1
    if(flag==0):
        continue
    for j in range(1,n+1):
        if(f[j]==0):
            break
        else:
            c+=1
    if(c==n):
        ind=i
        break
if(ind==-1):
    print(-1)
else:
    print(ind,end=" ")
    for i in range(n-1):
        ind+=a[i]
        print(ind,end=" ")


import copy
n=int(input())
a=list(map(int,input().split()))
a1=[0]*(n)
a1[0]=abs(a[0])
for i in range(1,n):
    a1[i]=a1[i-1]+a[i-1]
x=max(a1)
for i in range(n):
    a1[i]+=(n-x)
a2=copy.copy(a1)
a2.sort()
flag=1
for i in range(1,n+1):
    if(a2[i-1]==i):
        pass
    else:
        flag=0
if(flag==0):
    print(-1)
else:
    print(*a1)


n=int(input())
a=list(map(int,input().split()))
ind=-1
for i in range(1,n+1):
    f=[0]*(n+1)
    x=i
    f[x]=1
    flag=1
    c=0
    for j in range(0,n-1):
        x+=a[j]
        if(not (x>=1 and x<=n)):
            flag=0
            break
        if(f[x]==1):
            flag=0
            break
        else:
            f[x]=1
    if(flag==0):
        continue
    for j in range(1,n+1):
        if(f[j]==0):
            break
        else:
            c+=1
    if(c==n):
        ind=i
if(ind==-1):
    print(-1)
else:
    print(ind,end=" ")
    for i in range(n-1):
        ind+=a[i]
        print(ind,end=" ")


n=int(input())
a=[0]
for x in input().split():a+=a[-1]+int(x),
m=min(a)
a=[x-m+1for x in a]
print(' '.join(map(str,([-1],a)[set(a)==set(range(1,n+1))])))


n=int(input())
a=[0]
for x in input().split():a+=a[-1]+int(x),
m=min(a)
a=[x-m+1for x in a]
print(*([-1],a)[set(a)==set(range(1,n+1)))


n=int(input())
a=[0]
for x in input().split():a+=a[-1]+int(x),
a=[x-min(a)+1for x in a]
print(*([-1],a)[set(a)==set(range(1,n+1))])


from itertools import*
n=int(input())
a=[0,*accumulate(map(int,input().split()))]
m=min(a)
if set(a)==set(range(m,m+n)):print(*(x-m+1for x in a))
else:print(-1)


from itertools import*
n=int(input())
a=sorted(zip([0,*accumulate(map(int,input().split()))],range(n)))
if any(y-x-1for(x,_),(y,_)in zip(a,a[1:])):print(-1)
else:
 r=[0]*n
 for x,i in a:r[i]=x-a[0][0]+1
 print(*r)


from itertools import*
n=int(input())
a=[0,*accumulate(map(int,input().split()))]
m=min(a)
print(' '.join(str(x-m+1)for x in a)if set(a)==set(range(m,m+n))else -1)


n=int(input())
a=[0]
for x in input().split():a+=a[-1]+int(x),
m=min(a)
print(' '.join((['-1'],(str(x-m+1)for x in a))[max(a)-m+1==len(set(a))==n]))


n=int(input())
a=[0]
for x in input().split():a+=a[-1]+int(x),
m=min(a)
print(*([-1],[x-m+1for x in a])[set(a)==set(range(m,m+n))])


s=set(range(1,int(input())+1))
a=0,
for x in input().split():a+=a[-1]+int(x),
m=min(a)
a=[x-m+1for x in a]
print(*([-1],a)[set(a)==s])


n=int(input())
a=[0]
for x in input().split():a+=a[-1]+int(x),
m=min(a)
print(*([-1],(x-m+1for x in a))[set(a)==set(range(m,m+n))])


from itertools import*
n=int(input())
a=[0,*accumulate(map(int,input().split()))]
m=min(a)
print((-1,' '.join(str(x-m+1)for x in a))[sorted(a)==[*range(m,m+n)]])


s=set(range(1,int(input())+1))
a=[0]
for x in input().split():a+=a[-1]+int(x),
m=min(a)
a=[x-m+1for x in a]
print(*([-1],a)[set(a)==s])


from itertools import*
n=int(input())
a=[0,*accumulate(map(int,input().split()))]
m=min(a)
print((' '.join(str(x-m+1)for x in a),-1)[set(a)^set(range(m,m+n))])


from itertools import*
n=int(input())
a=[0,*accumulate(map(int,input().split()))]
m=min(a)
print((-1,' '.join(str(x-m+1)for x in a))[sorted(a)==[*range(m,m+n))]])


n=int(input())
a=[0]
for x in input().split():a+=a[-1]+int(x),
m=min(a)
a=[x-m+1for x in a]
print(*([-1],a)[set(a)==set(range(1,n+1))])


n=int(input())
a=[0]
for x in input().split():a+=a[-1]+int(x),
m=min(a)
a=[x-m for x in a]
print(*([-1],a)[set(a)==set(range(n))])


n=int(input())
a=[0]
for x in input().split():a+=a[-1]+int(x),
m=min(a)
print(*([-1],(x-m+1for x in a))[max(a)-m+1==len(set(a))==n])


n=int(input())
a=[0]
for x in input().split():a+=a[-1]+int(x),
m=min(a)-1
print(*([-1],(x-m for x in a))[max(a)-m==len(set(a))==n])


from itertools import*
n=int(input())
a=[0,*accumulate(map(int,input().split()))]
m=min(a)
print((-1,' '.join(str(x-m+1)for x in a))[set(a)==set(range(m,m+n))])


from itertools import*
n=int(input())
a=[0,*accumulate(map(int,input().split()))]
m=min(a)
print(-1if set(a).difference(range(m,m+n))else ' '.join(str(x-m+1)for x in a))


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))

n, = I()
q = I()
ans = [0]*n
ans[0] = 0
for i in range(1, n):
	ans[i] = q[i-1] + ans[i-1]
m = min(ans)
if m <= 0:
	m = abs(m) + 1
	for i in range(n):
		ans[i] = m + ans[i]

if set(a) == set(range(1, n+1)):
	print(*ans)
else:
	print(-1)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))

n, = I()
q = I()
ans = [0]*n
ans[0] = 0
for i in range(1, n):
	ans[i] = q[i-1] + ans[i-1]
m = min(ans)
if m <= 0:
	m = abs(m) + 1
	for i in range(n):
		ans[i] = m + ans[i]

if set(ans) == set(range(1, n+1)):
	print(*ans)
else:
	print(-1)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))

n, = I()
q = I()
ans = [0]*n
ans[0] = 0
m = min(ans)
if m <= 0:
	m = abs(m) + 1
	for i in range(n):
		ans[i] = m + ans[i]
cnt = [0]*(1000000)
for i in ans:
	cnt[i] += 1

if all(cnt[1:n+1]):
	print(*ans)
else:
	print(-1)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))

n, = I()
q = I()
ans = [0]*n
ans[0] = 0
for i in range(1, n):
	ans[i] = q[i-1] + ans[i-1]
m = min(ans)
if m <= 0:
	m = abs(m) + 1
	for i in range(n):
		ans[i] = m + ans[i]
cnt = [0]*(1000000)
for i in ans:
	cnt[i] += 1

if all(cnt[1:n+1]):
	print(*ans)
else:
	print(-1)


#include <iostream>
#include <algorithm>
using namespace std;
int b[100003],a[100003];
bool funk[100003]={};
pair <int,int> f[1000003];
int t=1;
int bin(int fi,int la,int k)
{
 int index;
 if (fi > la) index = -1;
 else
 {
    int mid = (fi + la)/2;
    if (k == f[mid].first && funk[f[mid].second] == false)
    {
        index = f[mid].second;
    }
    else if (k < f[mid].first) index = bin(fi, mid-1, k);
    else index = bin(mid+1, la, k);
 }
 return index;
 }
int main()
{
    int n,m,br=0;
    cin>>n>>m;
    for(int i=0;i<n;++i)
    {
        cin>>f[i].first;
        f[i].second = i;
    }
    for(int i=0;i<m;++i) cin>>b[i];
    sort(f,f+m);
    while(t==1&&br<m)
    {
        int idx = bin(0,n-1,b[br]);
        funk[idx] = true;
        if(idx == -1)
        {
            cout<<"Impossible";
        }
        else if(f[idx].first == f[idx+1].first) t=2;
        else a[br] = idx;
        br++;
    }
    if(t==2) cout<<"Ambiguity";
    else
    {
         cout<<"Possible"<<endl;
         for(int i=0;i<m;++i) cout<<a[i]+1<<" ";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int b[100003],a[100003];
pair <int,int> f[1000003];
int bin(int bot,int top,int k)
{
    if(bot>top)
        return -1;

    int mid=(bot+top)/2;

    if((mid==0 &&f[mid].first==k) || (f[mid].first==k && f[mid-1].first!=k))
        return mid;

    if(k<=f[mid].first)
    return bin(bot,mid-1,k);
    else if(k>f[mid].first)
    return bin(mid+1,top,k);
}
int main()
{
    int n,m,br=0;
    bool t=true,am=true;
    cin>>n>>m;
    for(int i=0;i<n;++i)
    {
        cin>>f[i].first;
        f[i].second = i;
    }
    for(int i=0;i<m;++i) cin>>b[i];
    sort(f,f+n);
    while(t==true&&br<m)
    {
        int idx = bin(0,n-1,b[br]);
        if(idx == -1)
        {
            t=false;
            cout<<"Impossible";
        }
        else if(idx<n-1 && f[idx].first == f[idx+1].first)
        {
            am=false;
        }
        else
            {
                a[br] = f[idx].second;
                br++;
            }
    }
    if(!am && t) cout<<"Ambiguity";
    else if(t && am)
    {
       cout<<"Possible"<<endl;
        for(int i=0;i<m;i++) cout<<a[i]+1;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int b[100003],a[100003];
bool funk[100003]={};
pair <int,int> f[1000003];
int t=1;
int bin(int fi,int la,int k)
{
 int index;
 if (fi > la) index = -1;
 else
 {
    int mid = (fi + la)/2;
    if (k == f[mid].first && funk[f[mid].second] == false)
    {
        index = f[mid].second;
    }
    else if (k < f[mid].first) index = bin(fi, mid-1, k);
    else index = bin(mid+1, la, k);
 }
 return index;
 }
int main()
{
    int n,m,br=0;
    cin>>n>>m;
    for(int i=0;i<n;++i)
    {
        cin>>f[i].first;
        f[i].second = i;
    }
    for(int i=0;i<m;++i) cin>>b[i];
    sort(f,f+m);
    while(t==1&&br<m)
    {
        int idx = bin(0,n-1,b[br]);
        funk[idx] = true;
        if(idx == -1)
        {
            cout<<"Impossible";
            t=0;
            break;
        }
        else if(f[idx].first == f[idx+1].first) t=2;
        else a[br] = idx;
        br++;
    }
    if(t==2) cout<<"Ambiguity";
    else
    {
        if(t!=0)
         {
            cout<<"Possible"<<endl;
            for(int i=0;i<m;++i) cout<<a[i]+1<<" ";
         }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int b[100003],a[100003];
bool funk[100003]={};
pair <int,int> f[1000003];
int t=1;
int bin(int fi,int la,int k)
{
 int index;
 if (fi > la) index = -1;
 else
 {
    int mid = (fi + la)/2;
    if (k == f[mid].first && funk == false)
    {
        index = f[mid].second;
    }
    else if (k < f[mid].first) index = bin(fi, mid-1, k);
    else index = bin(mid+1, la, k);
 }
 return index;
 }
int main()
{
    int n,m,br=0;
    cin>>n>>m;
    for(int i=0;i<n;++i)
    {
        cin>>f[i].first;
        f[i].second = i;
    }
    for(int i=0;i<m;++i) cin>>b[i];
    sort(f,f+m);
    while(t==1&&br<m)
    {
        int idx = bin(0,n,b[br]);
        funk[idx] = true;
        if(idx == -1) t=0;
        else if(f[idx].first == f[idx+1].first) t=2;
        else a[br] = idx;
        br++;
    }
    if(t==0) cout<<"Impossible";
    else if(t==2) cout<<"Ambiguity";
    else
    {
         cout<<"Possible"<<endl;
         for(int i=0;i<m;++i) cout<<a[i]+1<<" ";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int b[100003],a[100003];
bool funk[100003]={};
pair <int,int> f[1000003];
int t=1;
int bin(int bot,int top,int k)
{
    if(bot>top)
        return -1;

    int mid=(bot+top)/2;

    if((mid==0 &&f[mid].first==k) || (f[mid].first==k && f[mid-1].first!=k))
        return mid;

    if(k<=f[mid].first)
    return bin(bot,mid-1,k);
    else if(k>f[mid].first)
    return bin(mid+1,top,k);
}
int main()
{
    int n,m,br=0;
    cin>>n>>m;
    for(int i=0;i<n;++i)
    {
        cin>>f[i].first;
        f[i].second = i;
    }
    for(int i=0;i<m;++i) cin>>b[i];
    sort(f,f+n);
    while(t==1&&br<m)
    {
        int idx = bin(0,n-1,b[br]);
        funk[idx] = true;
        if(idx == -1) t=0;
        else if(f[idx].first == f[idx+1].first) t=2;
        else a[br] = f[idx].second;
        br++;
    }
    if(t==0) cout<<"Impossible"<<endl;
    else if(t==2) cout<<"Ambiguity";
    else
    {
            cout<<"Possible"<<endl;
            for(int i=0;i<m;++i) cout<<a[i]+1<<" ";
    }
    return 0;
}


#include <iostream>
using namespace std;
int b[100000],f[100000],a[100000];
bool funk[100000]={};
int main()
{
    int n,m,t=1,br=0;
    cin>>n>>m;
    for(int i=0;i<n;++i) cin>>f[i];
    for(int i=0;i<m;++i) cin>>b[i];
    while(t==1&&br<m)
    {
        int t1=0;
        for(int i=0;i<n;++i)
        {
            if(b[br]==f[i]&&funk[i]==false)
            {
                t1++;
                a[br] = i+1;
                funk[i]=true;
            }
        }
        br++;
        if(t1==0) t=0;
        if(t1>1) t=2;
    }
    if(t==0) cout<<"Impossible";
    else if(t==2) cout<<"Ambiguity";
    else
    {
         cout<<"Possible"<<endl;
         for(int i=0;i<m;++i) cout<<a[i]<<" ";
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int b[100003],a[100003];
pair <int,int> f[1000003];
int bin(int bot,int top,int k)
{
    if(bot>top)
        return -1;

    int mid=(bot+top)/2;

    if((mid==0 &&f[mid].first==k) || (f[mid].first==k && f[mid-1].first!=k))
        return mid;

    if(k<=f[mid].first)
    return bin(bot,mid-1,k);
    else if(k>f[mid].first)
    return bin(mid+1,top,k);
}
int main()
{
    ios_base::sync_with_stdio(false);
    int n,m,br=0;
    bool t=true,am=true;
    cin>>n>>m;
    for(int i=0;i<n;++i)
    {
        cin>>f[i].first;
        f[i].second = i;
    }
    for(int i=0;i<m;++i) cin>>b[i];
    sort(f,f+n);
    while(t==true&&br<m)
    {
        int idx = bin(0,n-1,b[br]);
        if(idx == -1)
        {
            t=false;
            cout<<"Impossible";
        }
        else if(idx<n-1 && f[idx].first == f[idx+1].first)
        {
            am=false;
        }
        else
            {
                a[br] = f[idx].second;
            }
        br++;
    }
    if(!am && t) cout<<"Ambiguity";
    else if(t && am)
    {
       cout<<"Possible"<<endl;
        for(int i=0;i<m;i++) cout<<a[i]+1<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define range 100005
int i,j,n,m,total1=0,total2=0,val1[range],val11[range],val2[range],val[range];
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++){scanf("%d",&val1[i]);val11[val1[i]]++;}
    for(i=1;i<=m;i++)scanf("%d",&val2[i]);
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(val2[i]==val1[j])
            {
                val[i]=j;
                total1++;
                break;
            }
        }
        if(val11[val2[i]]>1)total2++;
    }
    if(total1==m&&total2==0){printf("Possible\n");for(i=1;i<=m;i++)printf("%d%c",val[i],(i==m)?'\n':' ');}
    else if(total1==m&&total2>0)printf("Ambiguity\n");
    else printf("Impossible\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define range 100005
int i,j,n,m,total1=0,total2=0,val1[range],val11[range],val2[range],val[range],vall[range];
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++){scanf("%d",&val1[i]);val11[val1[i]]++;val[val1[i]]=i;}
    for(i=1;i<=m;i++)scanf("%d",&val2[i]);
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(val2[i]==val1[j])
            {
                total1++;
                break;
            }
        }
        vall[i]=val[val2[i]];
        if(val11[val2[i]]>1)total2++;
    }
    if(total1==m&&total2==0){printf("Possible\n");for(i=1;i<=m;i++)printf("%d%c",vall[i],(i==m)?'\n':' ');}
    else if(total1==m&&total2>0)printf("Ambiguity\n");
    else printf("Impossible\n");
    return 0;
}


#include <iostream>
using namespace std;
#define range 100005
long i,j,n,m,total1=0,total2=0,val1[range],val11[range],val2[range],val[range];
int main()
{
    cin>>n>>m;
    for(i=1;i<=max(n,m);i++)
    {
        if(i<=n){cin>>val1[i];val11[val1[i]]++;}
        if(i<=m)cin>>val2[i];
    }
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(val2[i]==val1[j])
            {
                val[i]=j;
                total1++;
                //val1[j]=0;
                break;
            }
        }
        if(val11[val2[i]]>1)total2++;
    }
    if(total1==m&&total2==0){cout<<"Possible"<<endl;for(i=1;i<=m;i++)cout<<val[i]<<' ';}
    else if(total1==m&&total2>0)cout<<"Ambiguity";
    else cout<<"Impossible";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define range 100005
int i,j,n,m,t,v1,val1[range],v2[range],v[range],val[range];
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++){scanf("%d",&v1);val1[v1]++;v[v1]=i;}
    for(i=1;i<=m;i++)
    {
        scanf("%d",&v2[i]);
        if(val1[v2[i]]==0){printf("Impossible\n");return 0;}
    }
    for(i=1;i<=m;i++)
    {
        if(val1[v2[i]]>1){printf("Ambiguity\n");return 0;}
        else val[i]=v[v2[i]];
    }
    printf("Possible\n");
    for(i=1;i<=m;i++)printf("%d%c",val[i],(i==m)?'\n':' ');
    return 0;
}


#include <iostream>
using namespace std;
#define range 100005
long i,j,n,m,total1=0,total2=0,val1[range],val11[range],val2[range],val[range];
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++){cin>>val1[i];val11[val1[i]]++;}
    for(i=1;i<=m;i++)cin>>val2[i];
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(val2[i]==val1[j])
            {
                val[i]=j;
                total1++;
                //val1[j]=0;
                break;
            }
        }
        if(val11[val2[i]]>1)total2++;
    }
    if(total1==m&&total2==0){cout<<"Possible"<<endl;for(i=1;i<=m;i++)cout<<val[i]<<' ';}
    else if(total1==m&&total2>0)cout<<"Ambiguity";
    else cout<<"Impossible";
    return 0;
}


#include <iostream>
using namespace std;
int range=100005;
int main()
{
    long i,j,n,m,total1=0,total2=0,val1[range],val11[range],val2[range],val[range];
    cin>>n>>m;
    for(i=1;i<=max(n,m);i++)
    {
        if(i<=n){cin>>val1[i];val11[val1[i]]++;}
        if(i<=m)cin>>val2[i];
    }
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(val2[i]==val1[j])
            {
                val[i]=j;
                total1++;
                //val1[j]=0;
                break;
            }
        }
        if(val11[val2[i]]>1)total2++;
    }
    if(total1==m&&total2==0){cout<<"Possible"<<endl;for(i=1;i<=m;i++)cout<<val[i]<<' ';}
    else if(total1==m&&total2>0)cout<<"Ambiguity";
    else cout<<"Impossible";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    long total1=0,total2=0,valf[100010],valb[100010],val1[100010]={0},val2[100010]={0},val[100010];
    long i,j,n,m;
    cin>>n>>m;
    for(i=1;i<=max(n,m);i++)
    {
        if(i<=n){cin>>valf[i];val1[valf[i]]++;}
        if(i<=m){cin>>valb[i];val2[valb[i]]++;}
    }
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(valb[i]==valf[j])
            {
                total1++;
                val[i]=j;
                valf[j]=0;
                break;
            }
            if(val1[valb[i]]>1)total2++;
        }
    }
    if(total1==m&&total2==0){cout<<"Possible"<<endl;for(i=1;i<=m;i++)cout<<val[i]<<' ';}
    else if(total1==m&&total2>0)cout<<"Ambiguity";
    else cout<<"Impossible";
    return 0;
}


#include <iostream>
using namespace std;
int range=100005;
int main()
{
    long i,j,n,m,total1=0,total2=0,val1[range],val11[range],val2[range],val[range];
    cin>>n>>m;
    for(i=1;i<=max(n,m);i++)
    {
        if(i<=n){cin>>val1[i];val11[val1[i]]++;}
        if(i<=m)cin>>val2[i];
    }
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(val2[i]==val1[j])
            {
                val[i]=j;
                total1++;
                val1[j]=0;
                break;
            }
        }
        if(val11[val2[i]]>1)total2++;
    }
    if(total1==m&&total2==0){cout<<"Possible"<<endl;for(i=1;i<=m;i++)cout<<val[i]<<' ';}
    else if(total1==m&&total2>0)cout<<"Ambiguity";
    else cout<<"Impossible";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define range 100005
int i,j,n,m,t,v1,val1[range],v2,v[range],val[range];
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++){scanf("%d",&v1);val1[v1]++;v[v1]=i;}
    for(i=1;i<=m;i++)
    {
        scanf("%d",&v2);
        if(val1[v2]==0){printf("Impossible\n");return 0;}
        else if(val1[v2]>1){printf("Ambiguity\n");return 0;}
        else val[i]=v[v2];
    }
    printf("Possible\n");
    for(i=1;i<=m;i++)printf("%d%c",val[i],(i==m)?'\n':' ');
    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int total1=0,total2=0;
    long n,m;
    cin>>n>>m;
    long val1[n],val11[10000]={0},val2[m],val22[10000]={0},val[m];
    for(i=0;i<max(n,m);i++)
    {
        if(i<n)
        {
            cin>>val1[i];
            val11[val1[i]]++;
        }
        if(i<m)
            cin>>val2[i];
    }
    for(long i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(val2[i]==val1[j])
                val[i]=j+1;
        }
    }
    for(long i=0;i<m;i++)
    {
        for(long j=0;j<n;j++)
        {
            if(val2[i]==val1[j])
            {
                if(val11[val1[j]]>1)
                    total2++;
                total1++;
                //val1[j]=0;
                break;
            }
        }
    }
    if(total1==m&&total2==0)
    {
        cout<<"Possible"<<endl;
        for(long i=0;i<m;i++)
            cout<<val[i]<<' ';
    }

    else if(total1==m&&total2>0)
        cout<<"Ambiguity";
    else
        cout<<"Impossible";
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <cmath>
#include <cstdlib>
#define sqr(x) (x)*(x)
using namespace std;

int main(){
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    int n,m,a[int(1e5+5)];
    pair<int,int> f[int(1e5+5)], b[int(1e5+5)];
    cin>>n>>m;
    for (int i=0; i<n; i++){
        cin>>f[i].first;
        f[i].second=i+1;
    }
    for (int i=0; i<m; i++){
        cin>>b[i].first;
        b[i].second=i+1;
    }
    sort(f,f+n);
    sort(b,b+m);
    int j=n-1;
    bool isOk=true;
//    for (int i=0; i<n; i++) cout<<f[i].first<<" "<<f[i].second<<endl;
//    for (int i=0; i<m; i++) cout<<b[i].first<<" "<<b[i].second<<endl;
    for (int i=m-1; i>=0 && j>=0; i--){
        while (j>=0 && b[i].first!=f[j].first) j--;
        if (j-1>=0 && f[j].first==f[j-1].first) isOk=false;
        a[b[i].second]=f[j].second;
    }
    if (j<0){
        cout<<"Impossible";
        return 0;
    }
    if (isOk==false){
        cout<<"Ambiguity";
        return 0;
    }
    cout<<"Possible"<<endl;
    for (int i=1; i<=m; i++) cout<<a[i]<<" ";
//    fclose(stdin);
//    fclose(stdout);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <cmath>
#include <cstdlib>
#define sqr(x) (x)*(x)
using namespace std;

int main(){
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    int n,m,a[int(1e5+5)];
    pair<int,int> f[int(1e5+5)], b[int(1e5+5)];
    cin>>n>>m;
    for (int i=0; i<n; i++){
        cin>>f[i].first;
        f[i].second=i+1;
    }
    for (int i=0; i<m; i++){
        cin>>b[i].first;
        b[i].second=i+1;
    }
    sort(f,f+n);
    sort(b,b+m);
    int j=m-1;
    for (int i=n-1; i>=0 && j>=0; i--){
        while (j>=0 && f[i].first!=b[j].first) j--;
        a[b[j].second]=f[i].second;
    }
    if (j<0){
        cout<<"Impossible";
        return 0;
    }
    for (int i=1; i<m; i++){
        if (b[i].first==b[i-1].first){
            cout<<"Ambiguity";
            return 0;
        }
    }
    cout<<"Possible"<<endl;
    for (int i=1; i<=m; i++) cout<<a[i]<<" ";
//    fclose(stdin);
//    fclose(stdout);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <cmath>
#include <cstdlib>
#define sqr(x) (x)*(x)
using namespace std;

int main(){
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    int n,m,a[int(1e5+5)];
    pair<int,int> f[int(1e5+5)], b[int(1e5+5)];
    cin>>n>>m;
    for (int i=0; i<n; i++){
        cin>>f[i].first;
        f[i].second=i+1;
    }
    for (int i=0; i<m; i++){
        cin>>b[i].first;
        b[i].second=i+1;
    }
    sort(f,f+n);
    sort(b,b+m);
    int j=n-1;
    bool isOk=true;
    for (int i=m-1; i>=0 && j>=0; i--){
        while (j>=0 && b[i].first!=f[j].first) j--;
        if (j-1>=0 && f[j].first==f[j-1].first) isOk=false;
        a[f[j].second]=b[i].second;
    }
    if (j<0){
        cout<<"Impossible";
        return 0;
    }
    if (isOk==false){
        cout<<"Ambiguity";
        return 0;
    }
    cout<<"Possible"<<endl;
    for (int i=1; i<=m; i++) cout<<a[i]<<" ";
//    fclose(stdin);
//    fclose(stdout);
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <cmath>
#include <cstdlib>
#define sqr(x) (x)*(x)
using namespace std;

int main(){
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    int n,m,a[int(1e5+5)];
    pair<int,int> f[int(1e5+5)], b[int(1e5+5)];
    cin>>n>>m;
    for (int i=0; i<n; i++){
        cin>>f[i].first;
        f[i].second=i+1;
    }
    for (int i=0; i<m; i++){
        cin>>b[i].first;
        b[i].second=i+1;
    }
    sort(f,f+n);
    sort(b,b+m);
    for (int i=0; i<n; i++){
        bool isFound=false;
        int l=0, r=m-1;
        while(l<=r){
            int mid=(l+r)/2;
            if (f[i].first==b[mid].first){
                isFound=true;
                a[b[mid].second]=f[i].second;
                break;
            }
            else if (f[i].first>b[mid].first) l=mid+1;
            else r=mid-1;
        }
        if (!isFound){
            cout<<"Impossible";
            return 0;
        }
    }
    for (int i=1; i<n; i++){
        if (f[i].first==f[i-1].first){
            cout<<"Ambiguity";
            return 0;
        }
    }
    cout<<"Possible"<<endl;
    for (int i=1; i<=m; i++) cout<<a[i]<<" ";
//    fclose(stdin);
//    fclose(stdout);
    return 0;
}