#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef unsigned long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

map<ll, ll> factorize(ll n) {  // O(ツ」n)
  map<ll, ll> res;
  for (int i=2; i*i<=n; ++i) {
    int cnt = 0;
    while (n%i==0) {
      n /= i;
      cnt++;
    }
    if (cnt) {
      res[i] = cnt;
    }
  }
  if (n!=1) res[n] = 1;
  return res;
}

int main() {
  int N; string M;
  int AAA = 0;
  while(cin >> N >> M, N) {
    ll num = strtoll(M.c_str(), NULL, N);
    map<ll, ll> factor = factorize(N);
    ll ans = 1LL<<62;
    FOR(it, factor) {
      ll now = it->first;
      ll cnt = 0;
      while(1) {
        cnt += num / now;
        if (now>num/it->first+EPS) break;
        now *= it->first;
      }
      //cout << it->first << " " << it->second << " " << cnt << endl;
       ans = min(ans, cnt/it->second);
    }
    cout << ans << endl;
  }
}
### Prompt

Construct a cpp code solution to the problem outlined:

You are one of ICPC participants and in charge of developing a library for multiprecision numbers and radix conversion. You have just finished writing the code, so next you have to test if it works correctly. You decided to write a simple, well-known factorial function for this purpose:

<image>

Your task is to write a program that shows the number of trailing zeros when you compute M! in base N, given N and M.



Input

The input contains multiple data sets. Each data set is described by one line in the format below:


N M


where N is a decimal number between 8 and 36 inclusive, and M is given in the string repre- sentation in base N. Exactly one white space character appears between them.

The string representation of M contains up to 12 characters in base N. In case N is greater than 10, capital letters A, B, C, ... may appear in the string representation, and they represent 10, 11, 12, ..., respectively.

The input is terminated by a line containing two zeros. You should not process this line.

Output

For each data set, output a line containing a decimal integer, which is equal to the number of trailing zeros in the string representation of M! in base N.

Example

Input

10 500
16 A
0 0


Output

124
2

### Response

```cpp
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef unsigned long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

map<ll, ll> factorize(ll n) {  // O(ツ」n)
  map<ll, ll> res;
  for (int i=2; i*i<=n; ++i) {
    int cnt = 0;
    while (n%i==0) {
      n /= i;
      cnt++;
    }
    if (cnt) {
      res[i] = cnt;
    }
  }
  if (n!=1) res[n] = 1;
  return res;
}

int main() {
  int N; string M;
  int AAA = 0;
  while(cin >> N >> M, N) {
    ll num = strtoll(M.c_str(), NULL, N);
    map<ll, ll> factor = factorize(N);
    ll ans = 1LL<<62;
    FOR(it, factor) {
      ll now = it->first;
      ll cnt = 0;
      while(1) {
        cnt += num / now;
        if (now>num/it->first+EPS) break;
        now *= it->first;
      }
      //cout << it->first << " " << it->second << " " << cnt << endl;
       ans = min(ans, cnt/it->second);
    }
    cout << ans << endl;
  }
}
```