#include <bits/stdc++.h>
using namespace std;
using namespace rel_ops;
const double PI = acos((double)-1);
int ts, ts2, ts3, ts4;
int n, m;
long long gcd(long long x, long long y) {
  long long t;
  for (; y != 0;) {
    t = x % y;
    x = y;
    y = t;
  }
  return x;
}
double sqr(double x) { return x * x; }
string s1, s2;
char ra[105], rb[105], rc[105];
int f1[27][105][105], f2[27][105][105];
string s;
int (*ff)[27][105][105];
void getfarray() {
  int i, j, k, l, t1, t2, t3, t4, t5, t6, t7, t8, t9, t, nw;
  int(&f)[27][105][105] = *ff;
  m = s.size();
  for (i = 1; i <= m; i++) f[s[i - 1] - 96][i][i] = 1;
  for (k = 2; k <= m; k++) {
    for (i = 1; i + k - 1 <= m; i++) {
      j = i + k - 1;
      for (l = 1; l <= n; l++) {
        if (i == 2 && j == 5 && ra[l] == 3) ts = 0;
        for (int x = i + 1; x <= j; x++) {
          if (f[ra[l]][i][j]) break;
          f[ra[l]][i][j] = f[rb[l]][i][x - 1] & f[rc[l]][x][j];
        }
      }
    }
  }
}
int h[105][105];
int main() {
  int i, j, k, l, t1, t2, t3, t4, t5, t6, t7, t8, t9, t, nw;
  int tt1, tt2, tt3, tt4;
  double u1, u2, u3, u4, u5, u6, u7, u8, u9;
  char c1, c2, c3;
  srand((unsigned)time(0));
  cin >> s1 >> s2;
  cin >> n;
  for (i = 1; i <= n; i++) {
    scanf("%*c");
    scanf("%c->%c%c", &ra[i], &rb[i], &rc[i]);
    ra[i] -= 96;
    rb[i] -= 96;
    rc[i] -= 96;
  }
  ff = &f1;
  s = s1;
  getfarray();
  ff = &f2;
  s = s2;
  getfarray();
  memset(h, 50, sizeof(h));
  h[0][0] = 0;
  for (i = 1; i <= s1.size(); i++)
    for (j = 1; j <= i; j++)
      for (int al1 = 1; al1 <= 26; al1++)
        if (f1[al1][j][i])
          for (k = 1; k <= s2.size(); k++)
            for (l = 1; l <= k; l++)
              if (f2[al1][l][k]) {
                h[i][k] = min(h[i][k], h[j - 1][l - 1] + 1);
                ts = 0;
              }
  cout << (h[s1.size()][s2.size()] > 10000000 ? -1 : h[s1.size()][s2.size()])
       << endl;
  return 0;
}
