#include <bits/stdc++.h>
using namespace std;
int n;
char s1[51], s2[51], op[50][5];
int memo1[50][50][26], memo2[50][50][26], memo3[50][50];
int check1(int l, int r, char c) {
  if (l == r) return s1[l] == c;
  int &ret = memo1[l][r][c - 'a'];
  if (ret == -1) {
    ret = 0;
    for (int i = l; i < r; ++i)
      for (int j = 0; j < n; ++j)
        if (op[j][0] == c && check1(l, i, op[j][3]) &&
            check1(i + 1, r, op[j][4]))
          ret = 1;
  }
  return ret;
}
int check2(int l, int r, char c) {
  if (l == r) return s2[l] == c;
  int &ret = memo2[l][r][c - 'a'];
  if (ret == -1) {
    ret = 0;
    for (int i = l; i < r; ++i)
      for (int j = 0; j < n; ++j)
        if (op[j][0] == c && check2(l, i, op[j][3]) &&
            check2(i + 1, r, op[j][4]))
          ret = 1;
  }
  return ret;
}
int solve(int pos1, int pos2) {
  if (pos1 == -1 && pos2 == -1) return 0;
  if (pos1 == -1 || pos2 == -1) return 51;
  int &ret = memo3[pos1][pos2];
  if (ret == -1) {
    ret = 51;
    for (int i = 0; i <= pos1; ++i)
      for (int j = 0; j <= pos2; ++j)
        for (char c = 'a'; c <= 'z'; ++c)
          if (check1(i, pos1, c) && check2(j, pos2, c))
            ret = min(ret, 1 + solve(i - 1, j - 1));
  }
  return ret;
}
int main() {
  scanf("%s %s", s1, s2);
  int L1 = strlen(s1), L2 = strlen(s2);
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%s", op[i]);
  memset(memo1, -1, sizeof memo1);
  memset(memo2, -1, sizeof memo2);
  memset(memo3, -1, sizeof memo3);
  int ans = solve(L1 - 1, L2 - 1);
  printf("%d\n", ans == 51 ? -1 : ans);
  return 0;
}
