#include <bits/stdc++.h>
using namespace std;
int f1[55][55][26], f2[55][55][26], n;
char s1[55], s2[55], ch[55][10];
void sol(char *s, int f[55][55][26]) {
  int m = strlen(s);
  for (int i = 0; i < m; i++) f[i][i][s[i] - 'a'] = 1;
  for (int L = 1; L <= m; L++)
    for (int i = 0; i + L - 1 < m; i++) {
      int j = i + L - 1;
      for (int k = i; k < j; k++)
        for (int p = 0; p < n; p++)
          if (f[i][k][ch[p][3] - 'a'] && f[k + 1][j][ch[p][4] - 'a'])
            f[i][j][ch[p][0] - 'a'] = 1;
    }
}
int dp[55][55];
const int Inf = 10000000;
int main() {
  scanf("%s%s", s1, s2);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%s", ch[i]);
  memset(f1, 0, sizeof(f1));
  memset(f2, 0, sizeof(f2));
  sol(s1, f1);
  sol(s2, f2);
  int m1 = strlen(s1), m2 = strlen(s2);
  for (int i = 0; i <= m1; i++)
    for (int j = 0; j <= m2; j++) dp[i][j] = Inf;
  dp[0][0] = 0;
  for (int i = 0; i < m1; i++)
    for (int j = 0; j < m2; j++)
      for (int c = 0; c < 26; c++)
        for (int k = i; k < m1; k++)
          if (f1[i][k][c])
            for (int p = j; p < m2; p++)
              if (f2[j][p][c])
                dp[k + 1][p + 1] = min(dp[k + 1][p + 1], dp[i][j] + 1);
  printf("%d\n", dp[m1][m2] >= Inf ? -1 : dp[m1][m2]);
  return 0;
}
