#include <bits/stdc++.h>
using namespace std;
const int NN = 112;
const int MM = 512;
string msg[NN][MM],pat;
int Len[NN],N,M;
int LPS[NN],aut[NN][2];
int G[NN][NN],K[NN][NN];
/*
G[i][j] -> next state if cur state i and string j encountered (state = match)
K[i][j] -> no of matches found if state = i, and string = j encountered
*/
bool alpha(string ch){ return (ch.size() <= 1 and (ch[0] == 'a' or ch[0] == 'b')); }
int conv(char x){
if (x == 'a' or x == 'b') return (int)(x-'a');
return (int)(x-'0');
}
int conv(string x){
if (alpha(x))
return conv(x[0]);
int ans = 0;
for(int i = 0; i < (int)x.size(); ++i)
ans = ans*10 + conv(x[i]);
assert(ans >= 1 and ans <= N);
return ans;
}
void build_kmp(int n){
int k = LPS[0] = 0;
for(int i = 1; i < n; ++i){
while(k > 0 and pat[k] != pat[i])
k = LPS[k-1];
LPS[i] = (k += (pat[k] == pat[i]));
}
}
void build_aut(int n){
for(int i = 0; i <= n; ++i) for(int k = 0; k < 2; ++k){
if (i > 0 and k != conv(pat[i]))
aut[i][k] = aut[LPS[i-1]][k];
else
aut[i][k] = i + (k == conv(pat[i]));
}
}
void build_nxt(int n,int m){
for(int i = 1; i <= n; ++i) for(int j = 0; j <= m; ++j){
int ans = j, cnt = 0;
for(int k = 0; k < Len[i]; ++k){
int inp = conv(msg[i][k]);
if (alpha(msg[i][k])){
ans = aut[ans][inp];
cnt += (ans == m);
} else {
cnt += K[ans][inp];
ans = G[ans][inp];
}
}
G[j][i] = ans, K[j][i] = cnt;
}
}
void solve(){
cin >> N >> pat;
M = (int)(pat.size());
pat.push_back('$');
for(int i = 1; i <= N; ++i){
cin >> Len[i];
for(int j = 0; j < Len[i]; ++j)
cin >> msg[i][j];
}
build_kmp(M);
build_aut(M);
build_nxt(N,M);
cout << K[0][N] << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCB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TW2ldID0gKGsgKz0gKHBhdFtrXSA9PSBwYXRbaV0pKTsKICAgIH0KfQoKdm9pZCBidWlsZF9hdXQoaW50IG4pewogICAgZm9yKGludCBpID0gMDsgaSA8PSBuOyArK2kpIGZvcihpbnQgayA9IDA7IGsgPCAyOyArK2spewogICAgICAgIGlmIChpID4gMCBhbmQgayAhPSBjb252KHBhdFtpXSkpCiAgICAgICAgICAgIGF1dFtpXVtrXSA9IGF1dFtMUFNbaS0xXV1ba107CiAgICAgICAgZWxzZQogICAgICAgICAgICBhdXRbaV1ba10gPSBpICsgKGsgPT0gY29udihwYXRbaV0pKTsKICAgIH0KfQoKdm9pZCBidWlsZF9ueHQoaW50IG4saW50IG0pewogICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyArK2kpIGZvcihpbnQgaiA9IDA7IGogPD0gbTsgKytqKXsKICAgICAgICBpbnQgYW5zID0gaiwgY250ID0gMDsKICAgICAgICBmb3IoaW50IGsgPSAwOyBrIDwgTGVuW2ldOyArK2spewogICAgICAgICAgICBpbnQgaW5wID0gY29udihtc2dbaV1ba10pOwogICAgICAgICAgICBpZiAoYWxwaGEobXNnW2ldW2tdKSl7CiAgICAgICAgICAgICAgICBhbnMgPSBhdXRbYW5zXVtpbnBdOwogICAgICAgICAgICAgICAgY250ICs9IChhbnMgPT0gbSk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBjbnQgKz0gS1thbnNdW2lucF07CiAgICAgICAgICAgICAgICBhbnMgPSBHW2Fuc11baW5wXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBHW2pdW2ldID0gYW5zLCBLW2pdW2ldID0gY250OwogICAgfQp9Cgp2b2lkIHNvbHZlKCl7CiAgICBjaW4gPj4gTiA+PiBwYXQ7CiAgICBNID0gKGludCkocGF0LnNpemUoKSk7CiAgICBwYXQucHVzaF9iYWNrKCckJyk7CgogICAgZm9yKGludCBpID0gMTsgaSA8PSBOOyArK2kpewogICAgICAgIGNpbiA+PiBMZW5baV07CiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IExlbltpXTsgKytqKQogICAgICAgICAgICBjaW4gPj4gbXNnW2ldW2pdOwogICAgfQoKICAgIGJ1aWxkX2ttcChNKTsKICAgIGJ1aWxkX2F1dChNKTsKICAgIGJ1aWxkX254dChOLE0pOwogICAgY291dCA8PCBLWzBdW05dIDw8ICJcbiI7Cn0KCmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBzb2x2ZSgpOwogICAgcmV0dXJuIDA7Cn0K