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

using ll = long long;
using vi = vector<int>;
using vll = vector<long long>;

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

constexpr long long mod = 1000000007;

#define rep(i, n) for (int i = 0; i < n; i++)

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

    int n;
    cin >> n;

    vector<string> v(n);
    rep(i, n) cin >> v[i];

    string ss = "atcoder";

    rep(i, n) {
        if (ss >= v[i]) {
            bool b = true;
            int ind = 0;
            rep(j, v[i].size()) {
                if (v[i][j] != 'a') {
                    if (v[i][j] > 't') {
                        ind = j - 1;
                    } else {
                        ind = j;
                    }
                    b = false;
                    break;
                }
            }

            if (b) {
                cout << -1 << endl;
            } else {
                cout << ind << endl;
            }
        } else {
            cout << 0 << endl;
        }
    }
}
#include <bits/stdc++.h>
using namespace std;
// overflow prob
#define int long long
#define p_queue priority_queue
#define all(n)  n.begin(),n.end()
#define gcd(x,y) __gcd(x,y)
#define findString(s,x) s.find(x)!=string::npos
#define maxe(x) *max_element(all(x))
#define mine(x) *min_element(all(x))
#define rall(n) n.rbegin(),n.rend()
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);cout << fixed << setprecision(10);
#define next cout<<"\n"
template<typename T> void debug(vector<T> &array, bool IsSorted) {
	if (IsSorted) sort(all(array));
	for (int i = 0 ; i < array.size() ; i++) {
		cout << array[i] << " ";
	}
	next;
}
template<typename T> T max(T &a, T &b) {
	return a > b ? a : b;
}
template<typename T> T min(T &a, T &b) {
	return a > b ? b : a;
}
template<typename T> T maxthree(T &a, T &b, T &c) {
	return max(a, max(b, c));
}
template<typename T> T minThree(T &a, T &b, T &c) {
	return min(a, min(b, c));
}
const double pi = 3.141592653589793238; const int  INF = 1e9; const int32_t M = 1e9 + 7; const int32_t MM = 998244353;
const int N = 1e5 + 5; const int32_t maxn = N;
void solve();
signed main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	FAST_IO
	int T = 1;
	// cin >> T;
	while (T--) solve();
}
//.........................................................MainCode..............................................................................
// solving for base


void solve() {
	string x;
	cin >> x;
	int m;
	cin >> m;
	if (x.size() == 1) {
		cout << (x[0] - '0' <= m ? '1' : '0');
		return;
	}
	int d = 0;
	for (int i = 0 ; i < x.size(); i++) {
		d = max(d, (int)x[i] - '0');
	}
	// max element
	int ng = 2e18;
	int ok = d;
	while (ng - ok > 1) {
		int mid = ok + (ng - ok) / 2;
		int res = 0;
		bool jdg = true;
		for (int i = 0 ; i < x.size() ; i++) {
			if (res >= 2e18 / mid) {
				ng = mid;
				jdg = false;
				break;
			}
			res *= mid;
			res += (x[i] - '0');
		}
		if (jdg) {
			if (res > m) ng = mid;
			else ok = mid;
		}
	}
	cout << ok - d << endl;
}