#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef string::iterator state;
#define EPS (1e-7)
const ll INF = 1e9 + 10;
#define PI (acos(-1))
//const ll mod = 1000000007;
bool chmin(ll &a, ll b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
ll factor(state &itr);
ll number(state &itr);
string S;
ll n;
ll ans;

ll pos[2050000];
ll num[2050000];

ll expression(state &itr) {
    ll now = factor(itr);
        //cerr << "E" << *itr << " " << now << endl;
    while(true) {
        //cerr << "e" << *itr << " " << now << endl;
        chmin(now, INF);
        if(*itr == '+') {
            itr++;
            now += factor(itr);
            continue;
        }
        return now;
    }
}

ll factor(state &itr) {
    ll now = number(itr);
    while(true) {
        //cerr << "F:" << *itr << endl;
        chmin(now, INF);
        if(*itr == '*') {
            itr++;
            now *= number(itr);
            continue;
        }
        return now;
    }
}

ll number(state &itr) {
    //cerr << "N:" << *itr << endl;
    ll index = distance(S.begin(), itr);
    if(*itr == '(') {
        itr++;
        ll now = expression(itr);
        pos[index] = distance(S.begin(), itr);
        num[index] = now;
        //cerr << index << " " << now << endl;
        itr++;
        return now;
    }
    pos[index] = index;
    num[index] = *itr - '0';
    return *(itr++) - '0';
}


void f(ll a) {
    ll nowval = num[a];
    ll lindex = a;
    ll rindex = pos[a];
    deque<ll> deq;
    deq.push_back(nowval);
    if(nowval == n) ans++;
    if(S[a] == '(') {
        f(a + 1);
    }
    ll lastturn = 0;
    while(true) {
        //cerr << lindex << " " << rindex << " " << nowval << " " << ans << " " << lastturn <<endl;
        rindex++;
        if(rindex >= S.size()) return;
        if(S[rindex] == ')') return;
        if(S[rindex] == '*') {
            rindex++;
            if(deq.empty()) {
                deq.push_back(1);
                nowval += 1;
            }
            nowval -= deq.back();
            deq.back() *= num[rindex];
            nowval += deq.back();
            if(num[rindex] == 1) ans += lastturn;
            else lastturn = 0;
        } else if(S[rindex] == '+') {
            rindex++;
            deq.push_back(num[rindex]);
            nowval += num[rindex];
            lastturn = 0;
        }
        if(S[rindex] == '(') {
            f(rindex + 1);
        }
        rindex = pos[rindex];
        while(nowval >= n) {
            if(lindex > rindex) {
                nowval = 0;
                deq.clear();
                break;
            }
            if(n == nowval) {
                ans++;
                lastturn++;
            }
            //cerr << "iterating l: " << lindex << " "<< rindex << " " << nowval << " " << lastturn << endl;
            ll val = num[lindex];
            lindex = pos[lindex] + 1;
            if(lindex >= S.size() || S[lindex] == '+') {
                lindex++;
                nowval -= deq.front();
                deq.pop_front();
            } else if(S[lindex] == '*') {
                lindex++;
                nowval -= deq.front();
                deq.front() /= val;
                nowval += deq.front();
            }
        }
    }
}


int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        cin >> n;
        if(n == 0) break;
        cin >> S;
        auto itr = S.begin();
        //cerr << "---" << n << "---" << endl;
        expression(itr);
        //cout << expression(itr) << endl;
        ans = 0;
        //for(int i = 0; i <= S.size(); i++) cerr << pos[i] << "/" << num[i] << " ";
        //cerr << endl;
        itr = S.begin();
        f(0);
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 44468