#include<iostream>
#include<vector>
#include<string>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
typedef long long ll;
string s,q;
int n;
int m;
class gem{
    public:
    string left,right,all;
    bool ok;
    ll cnt;
    gem(){
        ok =false;
        cnt =0;
    };
};
class KMP
{
public:
    string pattern;
    int plen;
    vector<int> table;
    KMP(const string s) : pattern(s), plen((int)pattern.size()), table(plen+1){
        table[0] = -1;
        int j = -1;
        for(int i = 0; i < plen; i++){
            while(j >= 0 && pattern[i] != pattern[j]) j = table[j];
            if(pattern[i+1] == pattern[++j]) table[i+1] = table[j];
            else table[i+1] = j;
        }
    }
    void search(const string& text, vector<int>& res){
        int head = 0, j = 0, tlen = (int)text.size();
        while(head + j < tlen){
            if(pattern[j] == text[head + j]){
                if(++j != plen) continue;
                res.push_back(head);
            }
            head += j - table[j], j = max(table[j], 0);
        }
    }
};

ll match_count(string &a,string &b){
   string t = a+b;
   KMP kmp(q);
   vector<int> res;
   kmp.search(t,res);
   return (ll)res.size();
}

gem concatenate(gem a,gem b){
    gem res;
    if(a.ok&&b.ok){
        res.ok = true;
        res.cnt = a.cnt+b.cnt;
        res.cnt += match_count(a.right,b.left);
        res.left = a.left;
        res.right = b.right;
    }else if(a.ok){
        res.left = a.left;
        res.cnt += a.cnt;
        res.cnt += match_count(a.right,b.all);
        res.right = a.right.substr((int)b.all.size(),m-1-b.all.size()) + b.all;
        res.ok = true;
    }else if(b.ok){
        res.ok = true;
        res.cnt += b.cnt;
        res.cnt += match_count(a.all,b.left);
        res.left = a.all + b.left.substr(0,m-1-a.all.size());
        res.right = b.right;
    }else{
        string aa = a.all;
        string bb = b.all;
        string c = aa+bb;
        if(aa.size()+bb.size()>=m){
            res.cnt = match_count(aa,bb);
            res.left = c.substr(0,m-1);
            res.right = c.substr((int)c.size()-m+1,m-1);
            res.ok = true;
        }else{
            res.all = c;
        }
    }
    return res;
}

gem calc(gem a,ll c){
    if(c==1)return a;
    if(c%2==0){
        gem  b = calc(a,c/2);
        return concatenate(b,b);
    }else{
        gem  b = calc(a,c/2);
        return concatenate(a,concatenate(b,b));
    }
    
}

gem genom(int &id){
    ll num = 0;
    while(isdigit(s[id])){
        num *=10;
        num += s[id]-'0';
        id++;
    }
    if(num==0)num=1;
    gem g;
    if(s[id]=='('){
        id++;
        g = genom(id);
        id++;
        g = calc(g,num);
    }else{
        if(m==1){
            g.ok = true;
            if(s[id]==q[0]){
               g.cnt++;
            }
        }else{
            g.all.push_back(s[id]);
        }
        g =calc(g,num);
    }
    if(id==n-1||s[id+1]==')'){
        return g;
    }else{
        id++;
        return concatenate(g,genom(id));
    }
}


int main(){
    while(cin >> s && s!="#"){
        cin >> q;
        n = s.size();
        m = q.size();
        int id = 0;
        gem res = genom(id);
        cout << res.cnt << endl;
    }
    return 0;
}


Measured memory_bytes: 4868