// embrace the struggle
#include <bits/stdc++.h>
#pragma optimize GCC("O3")
#pragma optimize GCC("Ofast")
using namespace std;
using ll = long long;
#define sz(st) int(st.size())
#define all(st) st.begin(), st.end()
const int N = 100 + 5;
ll dp[N][N];
vector<vector<string> > a;
int n;
string t;
bool check(int idx, const string &cur)
{
int i = 0;
while (idx < sz(t) && i < sz(cur)) {
if (cur[i] == t[idx]) {
idx++, i++;
} else break;
}
return i == sz(cur);
}
ll rec(int idx, int j)
{
if (j == sz(t)) return 0;
if (idx == n) return 1e9;
ll &ret = dp[idx][j];
if (ret != -1) return ret;
ret = rec(idx + 1, j);
int len = sz(t) - j;
for (const string &s: a[idx]) {
if (sz(s) > len) continue;
if (check(j, s)) {
ret = min(ret, 1 + rec(idx + 1, j + sz(s)));
}
}
return ret;
}
void Solve()
{
cin >> t;
cin >> n;
a = vector<vector<string> >(n);
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++) {
int m;
cin >> m;
string temp;
while (m--) {
cin >> temp;
a[i].push_back(temp);
}
}
ll ans = rec(0, 0);
if (ans == 1e9) cout << -1;
else cout << ans;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
for (int tc = 1; tc <= t; tc++) {
Solve();
cout << "\n";
}
return 0;
}
Ly8gZW1icmFjZSB0aGUgc3RydWdnbGUKCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgoKI3ByYWdtYSBvcHRpbWl6ZSBHQ0MoIk8zIikKI3ByYWdtYSBvcHRpbWl6ZSBHQ0MoIk9mYXN0IikKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIGxsID0gbG9uZyBsb25nOwoKI2RlZmluZSBzeihzdCkgaW50KHN0LnNpemUoKSkKI2RlZmluZSBhbGwoc3QpIHN0LmJlZ2luKCksIHN0LmVuZCgpCmNvbnN0IGludCBOID0gMTAwICsgNTsKbGwgZHBbTl1bTl07CnZlY3Rvcjx2ZWN0b3I8c3RyaW5nPiA+IGE7CmludCBuOwpzdHJpbmcgdDsKCmJvb2wgY2hlY2soaW50IGlkeCwgY29uc3Qgc3RyaW5nICZjdXIpCnsKICAgIGludCBpID0gMDsKICAgIHdoaWxlIChpZHggPCBzeih0KSAmJiBpIDwgc3ooY3VyKSkgewogICAgICAgIGlmIChjdXJbaV0gPT0gdFtpZHhdKSB7CiAgICAgICAgICAgIGlkeCsrLCBpKys7CiAgICAgICAgfSBlbHNlIGJyZWFrOwogICAgfQogICAgcmV0dXJuIGkgPT0gc3ooY3VyKTsKfQoKbGwgcmVjKGludCBpZHgsIGludCBqKQp7CiAgICBpZiAoaiA9PSBzeih0KSkgcmV0dXJuIDA7CiAgICBpZiAoaWR4ID09IG4pIHJldHVybiAxZTk7CgogICAgbGwgJnJldCA9IGRwW2lkeF1bal07CiAgICBpZiAocmV0ICE9IC0xKSByZXR1cm4gcmV0OwogICAgcmV0ID0gcmVjKGlkeCArIDEsIGopOwogICAgaW50IGxlbiA9IHN6KHQpIC0gajsKICAgIGZvciAoY29uc3Qgc3RyaW5nICZzOiBhW2lkeF0pIHsKICAgICAgICBpZiAoc3oocykgPiBsZW4pIGNvbnRpbnVlOwogICAgICAgIGlmIChjaGVjayhqLCBzKSkgewogICAgICAgICAgICByZXQgPSBtaW4ocmV0LCAxICsgcmVjKGlkeCArIDEsIGogKyBzeihzKSkpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiByZXQ7Cn0KCgp2b2lkIFNvbHZlKCkKewogICAgY2luID4+IHQ7CiAgICBjaW4gPj4gbjsKICAgIGEgPSB2ZWN0b3I8dmVjdG9yPHN0cmluZz4gPihuKTsKICAgIG1lbXNldChkcCwgLTEsIHNpemVvZihkcCkpOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgaW50IG07CiAgICAgICAgY2luID4+IG07CiAgICAgICAgc3RyaW5nIHRlbXA7CiAgICAgICAgd2hpbGUgKG0tLSkgewogICAgICAgICAgICBjaW4gPj4gdGVtcDsKICAgICAgICAgICAgYVtpXS5wdXNoX2JhY2sodGVtcCk7CiAgICAgICAgfQogICAgfQogICAgbGwgYW5zID0gcmVjKDAsIDApOwogICAgaWYgKGFucyA9PSAxZTkpIGNvdXQgPDwgLTE7CiAgICBlbHNlIGNvdXQgPDwgYW5zOwp9CgpzaWduZWQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShudWxscHRyKTsKICAgIGludCB0ID0gMTsKICAgIC8vY2luID4+IHQ7CiAgICBmb3IgKGludCB0YyA9IDE7IHRjIDw9IHQ7IHRjKyspIHsKICAgICAgICBTb2x2ZSgpOwogICAgICAgIGNvdXQgPDwgIlxuIjsKICAgIH0KICAgIHJldHVybiAwOwp9Cg==