#include<bits/stdc++.h>
#define forn(i,s,t) for(register int i=(s);i<=(t);++i)
#define form(i,s,t) for(register int i=(s);i>=(t);--i)
using namespace std;
typedef long long LL;
template<typename T>inline T Max(T A, T B) {return A>B?A:B;}
template<typename T>inline T Min(T A, T B) {return A<B?A:B;}
inline bool pd(char *s) {
	return (s[0] == 'A');
}
const int N = 1e4+3, M = 1e5+3;
int n; char s[N][100]; LL t[N], f[N];
int main() {
	scanf("%d", &n);
	forn(i,1,n) scanf("%s", s[i]);
	t[0] = f[0] = 1;
	forn(i,1,n) {
		if(pd(s[i])) {
			t[i] = t[i-1];
			f[i] = f[i-1]*2 + t[i-1];
		} else {
			t[i] = t[i-1]*2 + f[i-1];
			f[i] = f[i-1];
		}
	}
	printf("%lld\n", t[n]);
	return 0;
}
// and -> T & T  c
// or  -> T | F  
// 		  T | T
// 		  F | T
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false), cin.tie(0);
#define et cout<<endl;
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
    while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
const int N = 2e5+5;
const int M = 2e5+5;
const int mod = 1e9+7;
int t, n, k;
int g[N];
bool check(vector<ll> v, vector<ll> q)
{
    if(v.size() > q.size()) return false;
    if(v.size() == q.size())
    {
        for(int i = 0 ; i < v.size(); i ++)
        {
            if(v[i] > q[i]) return false;
            if(v[i] == q[i]) continue;
            if(v[i] < q[i]) return true;
        }
        return true;
    }
    return true;
}
void solve()
{
    string X; ll n;
    cin >> X >> n;
    int d = -1;
    vector<ll> v;
    for(int i = 0 ; i < X.length(); i ++) v.push_back(X[i] - '0'), d = max(d, X[i] - '0');
    ll l = d + 1, r = n;
    while(l < r)
    {
        ll mid = l + r + 1 >> 1;
        ll p = n;
        vector<ll> q;
        while(p)
        {
            q.push_back(p % mid);
            p /= mid;
        }
        reverse(q.begin(), q.end());
        if(check(v, q)) l = mid;
        else r = mid - 1;
    }
    if(v.size() == 1)
    {
        if(d <= n) cout << 1 << endl;
        else cout << 0 << endl;
        return;
    }
    if(l == d + 1)
    {
        ll p = n;
        vector<ll> q;
        while(p)
        {
            q.push_back(p % l);
            p /= l;
        }
        reverse(q.begin(), q.end());
        if(check(v, q)) cout << 1 << endl;
        else cout << 0 << endl;
    }
    else cout << l - d << endl;
}
int main()
{
    ios
    bool multi = 0;   // 0 for single and 1 for multi
    if(multi) cin >> t;
    else t = 1;
    while(t--)
    {
        solve();
    }
}