#include using namespace std; #define PII pair #define VI vector #define VPII vector #define LL long long #define f first #define s second #define MP make_pair #define PB push_back #define LD long double #define endl '\n' #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++) #define FORD(i, b, e) for (int i = (b); i >= (int)(e); i--) #define ll LL #define mp MP #define pb PB #define st f #define nd s #define eb emplace_back const int inf = 1e9 + 7; const LL INF = 1e18L + 7; #define sim template ostream & operator << (ostream &p, pair x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if::value, decltype(y.begin(), p)>::type {int o = 0; for (auto c : y) {if (o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if (p > y) p = y;} sim, class s> void maxi(n &p, s y) {if (p < y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #define A(a, i) #a "[" #i " =", i, "] =", a[i], " " const int MXN = 3e5+5; char buf[MXN]; char buf2[MXN]; int len[MXN]; LL h_i[MXN]; string in[MXN]; LL pot[MXN]; LL mod = 10000000000000861; int p = 193; LL h[MXN]; LL mul(LL a, LL b) { LL q = (LL)((LD)a * (LD)b / (LD)mod); LL r = a * b - q*mod; r %= mod; // if(r < 0)r += mod; // TODO: uwaga return r; } LL get_h(LL a, LL b) { LL x = (h[b] - mul(h[a-1], pot[b-a+1]))% mod; if(x < 0)x += mod; return x; } int min_poc[MXN]; const int MAGIC = 200; int m; void go_long(LL h, int len) { FOR(i, len, m) { if(get_h(i-len+1, i) == h) { mini(min_poc[i], i-len+1); } } } set S[MAGIC+1]; int dp[MXN]; const int BASE = 1<<19; int t[BASE*2]; void insert(int i, int x) { i += BASE; t[i] = x; i/=2; while(i) { t[i] = min(t[i*2], t[i*2+1]); i/=2; } } int query(int a, int b) { a += BASE-1; b += BASE+1; int r = 1e9; while(a/2 != b/2) { if(a % 2 == 0)mini(r, t[a+1]); if(b % 2 == 1)mini(r, t[b-1]); a /= 2; b /= 2; } return r; } int main() { int n; scanf("%d", &n); scanf(" %s", buf+1); m = strlen(buf+1); FOR(i, 1, m)min_poc[i] = 1e9; pot[0] = 1; FOR(i, 1, m) { h[i] = (h[i-1] * p + buf[i]) % mod; pot[i] = pot[i-1] * p % mod; } REP(i, n) { scanf("%s", buf2); int k = strlen(buf2); LL h = 0; REP(j, k) { h = (h * p + buf2[j]) % mod; } // len2[i] = k; // h_i[i] = h; if(k >= MAGIC) { go_long(h, k); } else { S[k].insert(h); } } dp[0] = 0; FOR(i, 1, m) dp[i] = 1e9; FOR(i, 0, m) insert(i, dp[i]); FOR(i, 1, m) { if(min_poc[i] > 1e8) FOR(j, max(1, i-MAGIC), i) { int l = i - j + 1; if(S[l].size() == 0)continue; LL h = get_h(j, i); if(S[l].count(h)) { mini(min_poc[i], j); break; } } if(min_poc[i] <= i) { int x = query(min_poc[i]-1, i) + 1; dp[i] = x; insert(i, x); } } if(dp[m] > 1e8) { puts("-1"); } else { cout << dp[m] << endl; } }