#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef bitset<16> BS;
struct edge {
    int to, cost, id;
};
const double EPS = 1E-09;
const ll MOD = 1E+09 + 7; // =998244353;
const ll INF = 1E18;
const int MAX_N = 1E+05;

ll dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };

ll M;

string X;

bool f(ll n);

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

    cin >> X >> M;

    ll d = 0;
    for (auto x : X) {
        d = max(d, (ll)(x - '0'));
    }

    reverse(X.begin(), X.end());

    ll high = M + 1, low = 0;
    ll ans = 0;
    //ll high = (ll)(pow((double)M / (double)(X[X.size() - 1]), 1.0 / ((double)X.size() - 1.0))) + 100
    if (X.size() > 1) {
        while (true) {
            ll mid = (high + low) / 2;
            if (mid == high || mid == low)
                break;
            if (f(mid)) {
                high = mid;
            } else {
                low = mid;
            }
            //cout << "high = " << high << ", low = " << low << "\n";
        }
    } else {
        if (X[0] - '0' <= M)
            ans = 1;
    }

    /*
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";
        }
    }
    */

    cout << (X.size() == 1 ? ans : max(0LL, low - d)) << "\n";

    return 0;
}

// bool f(ll n)
// {
//     ll res = 0;
//     for (auto c : X) {
//         ll x = c - '0';
//         if (res > M / n)
//             return true;
//         res = res * n + x;
//     }
//     //cout << "n = " << n << ", res = " << res << "\n";
//     return res > M;
// }

bool f(ll n)
{
    ll res = 0;
    ll d = 1;
    for (int i = 0; i < X.size(); i++) {
        ll x = X[i] - '0';
        //cout << "xd = " << x * d << ", res = " << res << "\n";
        if (x > (M - res) / d)
            return true;
        res += x * d;
        if (n > M / d && i < (int)X.size() - 1)
            return true;
        d *= n;
    }
    return res > M;
}

// bool f(ll n)
// {
//     vector<ll> res;
//     ll m = M;
//     while (m > 0) {
//         res.push_back(m % n);
//         m /= n;
//     }
//     // cout << "n = " << n << "\n";
//     // cout << "Xsize = " << X.size() << ", res.size = " << res.size() << "\n";
//     if (res.size() != X.size())
//         return X.size() > res.size();

//     reverse(res.begin(), res.end());
//     for (int i = 0; i < X.size(); i++) {
//         ll x = X[i] - '0';
//         if (x > res[i])
//             return true;
//         else if (x < res[i])
//             return false;
//     }
//     return false;
// }
#include <bits/stdc++.h>

using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int,int>;
using ll_P = pair<ll,ll>;
const double PI=3.14159265358979323846;
const int INF = 1000000000;
const ll ll_INF = 1001001001001001001;
const int mod = 1000000007;

int n;
vector<int> p(200005);

int main(){
    cin >> n;
    rep(i,n){
        cin >> p[i];
    }

    int now = 1;
    int last_sorted = 0;

    vector<int> ans;

    rep(i,n){
        if(p[i]==now){
            if(i==last_sorted){
                if(p[i]==n){
                    break;
                }
                cout << -1 << endl;
                return 0;
            }
            for(int j=i;j>last_sorted;j--){
                p[j] = p[j-1];
                ans.push_back(j);
            }
            p[last_sorted] = now;
            now = i+1;
            last_sorted = i;
            i--;
        }
    }
    bool flag = true;
    rep(i,n){
        if(p[i] != i+1) flag = false;
    }

    if(!flag){
        cout << -1 << endl;
    }
    else{
        rep(i,n-1){
            cout << ans[i] << endl;
        }
    }
}