#include <bits/stdc++.h>

const int alphabet = 30;
const int maxn = 1e6 + 10;

int n;
char go[alphabet];
char s[maxn], t[maxn];
int jump[alphabet];

int main() {
    freopen("maniac.in", "r", stdin);
    freopen("maniac.out", "w", stdout);
    gets(go);
    scanf("%d ", &n);
    gets(s);
    gets(t);
    for (int i = 0; i < 26; i++)
        go[i] -= 'a';
    for (int i = 0; i < n; i++)
        s[i] -= 'a', t[i] -= 'a';

    for (int i = 0; i < alphabet; i++)
        jump[i] = -1;
    for (int i = 0; i < n; i++) {
        if (jump[(int)s[i]] == -1)
            jump[(int)s[i]] = t[i];
        if (jump[(int)s[i]] != t[i]) {
            printf("-1\n");
            return 0;
        }
    }

    for (int i = 0; i < alphabet; i++)
        s[i] = i;
    int iteration = 0;
    int counter = 0;
    while (true) {
        if (++counter == 100)
            counter = 0;
        if (counter == 0 && clock() >= 480) {
            printf("-1\n");
            return 0;
        }
        bool ok = true;
        for (int i = 0; i < alphabet; i++) {
             if (jump[i] != -1 && jump[i] != s[i])
                ok = false;
        }
        if (ok) {
            printf("%d\n", iteration);
            return 0;
        }
        for (int i = 0; i < n; i++)
            s[i] = go[(int)s[i]];
        ++iteration;
    }
    return 0;
}
