#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
int main() {
    ll k, x;
    cin >> k >> x;
    
    ll power = 1;
    ll res = INF;
    
    for(int i = 0; i < 100; i++) {
        ll tmp = power * k;
        if(tmp <= x) {
            if(x % tmp == 0) {
                ll quotient = x / tmp;
                if(quotient * k * power == x) {
                    res = min(res, i + quotient);
                }
            }
        }
        else {
            break;
        }
        power *= k;
    }
    if(res == INF) {
        res = -1;
    }
    cout << res << endl;
    
    return 0;
}
