#include <bits/stdc++.h>
using namespace std;
string a, b;
vector<string> v[27];
char aux[7];
bool dp1[55][55][30];
bool dp2[55][55][30];
int dp[55][55];
void citire() {
  int n;
  cin >> a;
  cin >> b;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> aux;
    string temp;
    temp = aux[3];
    temp += aux[4];
    v[aux[0] - 'a'].push_back(temp);
  }
}
void dinamiciAjutatoare() {
  for (int i = 1; i <= a.size(); i++) {
    dp1[i][i][a[i - 1] - 'a'] = true;
    for (int j = i - 1; j >= 1; j--) {
      for (int c = 0; c <= 'z' - 'a'; c++) {
        for (int p = 0; p < v[c].size(); p++) {
          for (int k = j; k < i; k++) {
            dp1[j][i][c] |= (dp1[j][k][v[c][p][0] - 'a'] &&
                             dp1[k + 1][i][v[c][p][1] - 'a']);
          }
        }
      }
    }
  }
  for (int i = 1; i <= b.size(); i++) {
    dp2[i][i][b[i - 1] - 'a'] = true;
    for (int j = i - 1; j >= 1; j--) {
      for (int c = 0; c <= 'z' - 'a'; c++) {
        for (int p = 0; p < v[c].size(); p++) {
          for (int k = j; k < i; k++) {
            dp2[j][i][c] |= (dp2[j][k][v[c][p][0] - 'a'] &&
                             dp2[k + 1][i][v[c][p][1] - 'a']);
          }
        }
      }
    }
  }
}
void solveDP() {
  for (int i = 1; i <= a.size(); i++) {
    for (int j = 1; j <= b.size(); j++) {
      for (int c = 0; c <= 'z' - 'a'; c++) {
        dp[i][j] |= (dp1[1][i][c] && dp2[1][j][c]);
      }
      if (dp[i][j] == 1) continue;
      for (int i2 = 1; i2 < i; i2++) {
        for (int j2 = 1; j2 < j; j2++) {
          for (int c = 0; c <= 'z' - 'a'; c++) {
            if (dp[i2][j2] == 0 ||
                (dp1[i2 + 1][i][c] && dp2[j2 + 1][j][c]) == false)
              continue;
            if (dp[i][j] == 0 || dp[i2][j2] + 1 < dp[i][j]) {
              dp[i][j] = dp[i2][j2] + 1;
            }
          }
        }
      }
    }
  }
}
void afisareSolutie() {
  if (dp[a.size()][b.size()] == 0)
    cout << -1;
  else
    cout << dp[a.size()][b.size()];
}
int main() {
  citire();
  dinamiciAjutatoare();
  solveDP();
  afisareSolutie();
  return 0;
}
