#include <bits/stdc++.h>
using namespace std;
char s[50 | 1];
int n, m, c, a[50 | 1], b[50 | 1], x[50 | 1], y[50 | 1], z[50 | 1],
    f[50 | 1][50 | 1], g[50 | 1][50 | 1][26 | 1], h[50 | 1][50 | 1][26 | 1];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) a[i] = s[i] - 'a' + 1;
  scanf("%s", s + 1);
  m = strlen(s + 1);
  for (int i = 1; i <= m; i++) b[i] = s[i] - 'a' + 1;
  scanf("%d", &c);
  for (int i = 1; i <= c; i++) {
    scanf("%s", s + 1);
    x[i] = s[1] - 'a' + 1;
    y[i] = s[4] - 'a' + 1;
    z[i] = s[5] - 'a' + 1;
  }
  for (int i = 1; i <= n; i++) g[i][i][a[i]] = 1;
  for (int i = 1; i <= m; i++) h[i][i][b[i]] = 1;
  for (int l = 1; l < n; l++)
    for (int i = 1, j = i + l; j <= n; i++, j++)
      for (int k = i; k < j; k++)
        for (int ev = 1; ev <= c; ev++)
          if (g[i][k][y[ev]] && g[k + 1][j][z[ev]]) g[i][j][x[ev]] = 1;
  for (int l = 1; l < m; l++)
    for (int i = 1, j = i + l; j <= m; i++, j++)
      for (int k = i; k < j; k++)
        for (int ev = 1; ev <= c; ev++)
          if (h[i][k][y[ev]] && h[k + 1][j][z[ev]]) h[i][j][x[ev]] = 1;
  memset(f, 0x3f, sizeof(f));
  f[0][0] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int l = 1; l <= i; l++)
        for (int r = 1; r <= j; r++)
          for (int ch = 1; ch <= 26; ch++)
            if (g[l][i][ch] && h[r][j][ch])
              f[i][j] = min(f[i][j], f[l - 1][r - 1] + 1);
  printf("%d\n", f[n][m] < 0x3f3f3f3f ? f[n][m] : -1);
  return 0;
}
