#include <bits/stdc++.h>
using namespace std;
int i, j, k, l, n, m, s, an, sum;
const int N = 72;
class arr {
 public:
  int x, w1, w2;
} t[N];
bool ff[N][N][30], fp[N][N][30];
int f[N][N];
char a[N], b[N];
int la, lb;
int main() {
  scanf("%s", a + 1);
  la = strlen(a + 1);
  getchar();
  scanf("%s", b + 1);
  lb = strlen(b + 1);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    getchar();
    char ch;
    ch = getchar();
    t[i].x = ch - 'a';
    getchar();
    getchar();
    ch = getchar();
    t[i].w1 = ch - 'a';
    ch = getchar();
    t[i].w2 = ch - 'a';
  }
  for (int i = 1; i <= la; i++) ff[i][i][a[i] - 'a'] = true;
  for (int l = 2; l <= la; l++)
    for (int i = 1; i + l - 1 <= la; i++)
      for (int k = i; k < i + l - 1; k++)
        for (int p = 1; p <= n; p++)
          if ((ff[i][k][t[p].w1] && ff[k + 1][i + l - 1][t[p].w2]))
            ff[i][i + l - 1][t[p].x] = true;
  for (int i = 1; i <= lb; i++) fp[i][i][b[i] - 'a'] = true;
  for (int l = 2; l <= lb; l++)
    for (int i = 1; i + l - 1 <= lb; i++)
      for (int k = i; k < i + l - 1; k++)
        for (int p = 1; p <= n; p++)
          if ((fp[i][k][t[p].w1] && fp[k + 1][i + l - 1][t[p].w2]))
            fp[i][i + l - 1][t[p].x] = true;
  memset(f, 2, sizeof(f));
  f[1][1] = 0;
  for (int i = 1; i <= la; i++)
    for (int j = 1; j <= lb; j++) {
      for (int ir = i; ir <= la; ir++)
        for (int jr = j; jr <= lb; jr++)
          for (int k = 0; k < 27; k++)
            if (ff[i][ir][k] && fp[j][jr][k])
              f[ir + 1][jr + 1] = min(f[ir + 1][jr + 1], f[i][j] + 1);
    }
  if (f[la + 1][lb + 1] == 33686018)
    printf("-1");
  else
    printf("%d", f[la + 1][lb + 1]);
  return 0;
}
