#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int maxn = 60;
#pragma GCC Optimize("Ofast")
bool cn[2][maxn][maxn][30];
int dp[maxn][maxn];
set<pair<int, pair<int, int> > > st;
bool is[30][30][30];
bool equal(int a1, int a2, int b1, int b2) {
  for (int i = 0; i < 26; i++)
    if (cn[0][a1][a2][i] && cn[1][b1][b2][i]) return 1;
  return 0;
}
void FILL(string &s, int id) {
  for (int i = 0; i < (s).size(); i++) cn[id][i][i][s[i] - 'a'] = 1;
  for (int j = 1; j <= (s).size() - 1; j++) {
    for (int i = j - 1; i >= 0; i--) {
      for (int p = i + 1; p <= j; p++) {
        for (int q = 0; q < 26; q++) {
          for (int q1 = 0; q1 < 26; q1++) {
            for (int q2 = 0; q2 < 26; q2++) {
              cn[id][i][j][q] |=
                  (cn[id][i][p - 1][q1] && cn[id][p][j][q2] && is[q][q1][q2]);
            }
          }
        }
      }
    }
  }
}
int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    is[s[0] - 'a'][s[3] - 'a'][s[4] - 'a'] = 1;
  }
  FILL(s1, 0);
  FILL(s2, 1);
  for (int i = 0; i < (s1).size(); i++) {
    for (int j = 0; j < (s2).size(); j++) {
      dp[i][j] = inf;
      if (equal(0, i, 0, j)) dp[i][j] = 1;
      for (int pi = 1; pi <= i; pi++) {
        for (int pj = 1; pj <= j; pj++) {
          if (equal(pi, i, pj, j))
            dp[i][j] = min(dp[i][j], 1 + dp[pi - 1][pj - 1]);
        }
      }
    }
  }
  cout << (dp[(s1).size() - 1][(s2).size() - 1] == inf
               ? -1
               : dp[(s1).size() - 1][(s2).size() - 1]);
}
