#include <bits/stdc++.h>
using namespace std;
int DP[2][50][50];
int len[2];
int convert[50][3];
int sol[50][50];
int solve(int a, int b) {
  if (a == len[0] && b == len[1]) return 0;
  if (a == len[0] || b == len[1]) return 100;
  if (sol[a][b] != -1) return sol[a][b];
  int res = 100;
  for (int i = a; i < len[0]; i++)
    for (int j = b; j < len[1]; j++) {
      if (DP[0][a][i] & DP[1][b][j]) {
        res = min(solve(i + 1, j + 1) + 1, res);
      }
    }
  sol[a][b] = res;
  return res;
}
int main() {
  memset(DP, 0, sizeof(DP));
  memset(sol, -1, sizeof(sol));
  for (int i = 0; i < 2; i++) {
    string str;
    cin >> str;
    len[i] = str.length();
    for (int j = 0; j < str.length(); j++) DP[i][j][j] = (1 << (str[j] - 'a'));
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    convert[i][0] = (1 << (str[0] - 'a'));
    convert[i][1] = (1 << (str[3] - 'a'));
    convert[i][2] = (1 << (str[4] - 'a'));
  }
  bool flag = true;
  while (flag) {
    flag = false;
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < len[i]; j++)
        for (int k = 0; k + j < len[i]; k++)
          for (int l = 0; j + k + l < len[i]; l++) {
            for (int m = 0; m < n; m++)
              if ((DP[i][l][l + j] & convert[m][1]) &&
                  (DP[i][l + j + 1][l + j + k + 1] & convert[m][2])) {
                if (DP[i][l][l + j + k + 1] & convert[m][0]) {
                } else {
                  DP[i][l][l + j + k + 1] |= convert[m][0];
                  flag = true;
                }
              }
          }
  }
  int res = solve(0, 0);
  if (res >= 100)
    cout << "-1";
  else
    cout << res;
}
