#include<iostream>
#include<cstdio>
#include<cassert>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<vector> 
#include<cstring>
#include<string>
#include<map>
#include<set>

#define mp make_pair
#define scn second
#define frs first
#define pb push_back

#define NAME "maniac"

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<ll, int> pi;
const int P = 239017;
const ll INF = 10000000000000000ll;

char nxt[200];
char a[1000001], b[1000001];
int len;
int go[200];
int now[200];


int main(){
    double start = clock();
    freopen(NAME ".in", "r", stdin); 
    freopen(NAME ".out", "w", stdout); 
    scanf("%s", nxt + 'a');
    scanf("%d%s%s", &len, a, b);
    assert(len <= 1000);

    for (int i = 0; i < len; ++i) {
        if (!go[a[i]])
            go[a[i]] = b[i];
        if (go[a[i]] != b[i]) {
            printf("-1");
            return 0;
        }
    }
    

    for (int i = 'a'; i <= 'z'; ++i)
        now[i] = i;


    int it = 1000000;


    int t = 0;
    while (it --> 0) {
        int good = true;
        for (int i = 0; i < len; ++i)
            good &= a[i] == b[i];
        if (good) {
            printf("%d", t);
            cerr << "\n" << (clock() - start) / CLOCKS_PER_SEC << "\n";
            return 0;
        }
        for (int i = 0; i < len; ++i)
            a[i] = nxt[a[i]];
        ++t;    
    }
    printf("-1");
    
    
    cerr << "\n" << (clock() - start) / CLOCKS_PER_SEC << "\n";
    
    return 0;
}




