#include <bits/stdc++.h>
using namespace std;
map<pair<char, char>, char> hash;
char c, x, y, s1[101], s2[101];
int f[101][101];
int a[61][61], b[61][61];
vector<pair<char, char> > p[31];
int n;
int main() {
  scanf("%s%s%d", s1, s2, &n);
  int l1 = strlen(s1);
  int l2 = strlen(s2);
  for (int i = 1; i <= n; i++) {
    getchar();
    c = getchar();
    getchar();
    getchar();
    x = getchar();
    y = getchar();
    p[c - 'a'].push_back(pair<char, char>(x - 'a', y - 'a'));
  }
  for (int i = 0; i <= l1 - 1; i++) a[i + 1][i + 1] = 1 << s1[i] - 'a';
  for (int i = 0; i <= l2 - 1; i++) b[i + 1][i + 1] = 1 << s2[i] - 'a';
  for (int len = 1; len <= l1; len++)
    for (int i = 1; i <= l1 - len + 1; i++) {
      int j = i + len - 1;
      for (int k = 0; k <= 25; k++)
        for (int is = 0; is < p[k].size(); is++) {
          for (int t = i; t <= j - 1; t++)
            if ((a[i][t] & 1 << p[k][is].first) &&
                (a[t + 1][j] & 1 << p[k][is].second)) {
              a[i][j] |= 1 << k;
              break;
            }
          if (a[i][j] & 1 << k) break;
        }
    }
  for (int len = 1; len <= l2; len++)
    for (int i = 1; i <= l2 - len + 1; i++) {
      int j = i + len - 1;
      for (int k = 0; k <= 25; k++)
        for (int is = 0; is < p[k].size(); is++) {
          for (int t = i; t <= j - 1; t++)
            if ((b[i][t] & 1 << p[k][is].first) &&
                (b[t + 1][j] & 1 << p[k][is].second)) {
              b[i][j] |= 1 << k;
              break;
            }
          if (b[i][j] & 1 << k) break;
        }
    }
  memset(f, 120, sizeof(f));
  f[0][0] = 0;
  for (int i = 1; i <= l1; i++)
    for (int j = 1; j <= l2; j++)
      for (int i1 = 0; i1 <= i - 1; i1++)
        for (int j1 = 0; j1 <= j - 1; j1++)
          if ((a[i1 + 1][i] & b[j1 + 1][j]) != 0)
            f[i][j] = min(f[i][j], f[i1][j1] + 1);
  printf("%d\n", f[l1][l2] < 100 ? f[l1][l2] : -1);
  fclose(stdin);
  return 0;
}
