#include <bits/stdc++.h>
int N, M, K, g[55][55], h[55][55], f[55][55], a[55][3];
char A[55], B[55], s[55];
int main() {
  scanf("%s%s%d", A + 1, B + 1, &K), N = strlen(A + 1), M = strlen(B + 1);
  for (int i = 1; i <= K; i++)
    scanf("%s", s), a[i][0] = s[3] - 97, a[i][1] = s[4] - 97,
                    a[i][2] = s[0] - 97;
  for (int i = 1; i <= N || i <= M; i++)
    g[i][i] = 1 << A[i] - 97, h[i][i] = 1 << B[i] - 97;
  for (int l = 2; l <= N; l++)
    for (int i = 1, j; (j = i + l - 1) <= N; i++)
      for (int k = i; k < j; k++)
        for (int t = 1; t <= K; t++)
          if ((g[i][k] >> a[t][0]) & 1 && (g[k + 1][j] >> a[t][1]) & 1)
            g[i][j] |= 1 << a[t][2];
  for (int l = 2; l <= N; l++)
    for (int i = 1, j; (j = i + l - 1) <= M; i++)
      for (int k = i; k < j; k++)
        for (int t = 1; t <= K; t++)
          if ((h[i][k] >> a[t][0]) & 1 && (h[k + 1][j] >> a[t][1]) & 1)
            h[i][j] |= 1 << a[t][2];
  memset(f, 63, sizeof(f)), f[0][0] = 0;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++)
      for (int x = 0; x < i; x++)
        for (int y = 0; y < j; y++)
          if (g[x + 1][i] & h[y + 1][j] && f[i][j] > f[x][y] + 1)
            f[i][j] = f[x][y] + 1;
  printf("%d\n", f[N][M] > 1e9 ? -1 : f[N][M]);
}
