#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
const int inf = 1e9 + 10;
string s1, s2;
int n, lens1, lens2;
vector<int> g[30][30];
vector<pair<int, int> > v1[30], v2[30];
bool ok[maxn][maxn][maxn][maxn], f1[30][maxn][maxn], f2[30][maxn][maxn];
int dp[maxn][maxn];
int main() {
  cin >> s1 >> s2 >> n;
  lens1 = (int)s1.size();
  lens2 = (int)s2.size();
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    g[s[3] - 'a'][s[4] - 'a'].push_back(s[0] - 'a');
  }
  for (int i = 0; i < (int)s1.size(); i++) f1[s1[i] - 'a'][i][i] = true;
  for (int len = 2; len <= (int)s1.size(); len++) {
    for (int l = 0; l + len - 1 < (int)s1.size(); l++) {
      int r = l + len - 1;
      for (int mid = l; mid + 1 <= r; mid++) {
        for (int lc = 0; lc < 26; lc++)
          for (int rc = 0; rc < 26; rc++) {
            for (int i = 0; i < g[lc][rc].size(); i++) {
              int c = g[lc][rc][i];
              f1[c][l][r] |= f1[lc][l][mid] & f1[rc][mid + 1][r];
            }
          }
      }
    }
  }
  for (int i = 0; i < (int)s2.size(); i++) f2[s2[i] - 'a'][i][i] = true;
  for (int len = 2; len <= (int)s2.size(); len++) {
    for (int l = 0; l + len - 1 < (int)s2.size(); l++) {
      int r = l + len - 1;
      for (int mid = l; mid + 1 <= r; mid++) {
        for (int lc = 0; lc < 26; lc++)
          for (int rc = 0; rc < 26; rc++) {
            for (int i = 0; i < g[lc][rc].size(); i++) {
              int c = g[lc][rc][i];
              f2[c][l][r] |= f2[lc][l][mid] & f2[rc][mid + 1][r];
            }
          }
      }
    }
  }
  for (int k = 0; k < 26; k++)
    for (int i = 0; i < (int)s1.size(); i++)
      for (int j = i; j < (int)s1.size(); j++)
        if (f1[k][i][j]) v1[k].push_back(make_pair(i, j));
  for (int k = 0; k < 26; k++)
    for (int i = 0; i < (int)s2.size(); i++)
      for (int j = i; j < (int)s2.size(); j++)
        if (f2[k][i][j]) v2[k].push_back(make_pair(i, j));
  for (int c = 0; c < 26; c++) {
    for (int i = 0; i < v1[c].size(); i++) {
      int l1 = v1[c][i].first, r1 = v1[c][i].second;
      for (int j = 0; j < v2[c].size(); j++) {
        int l2 = v2[c][j].first, r2 = v2[c][j].second;
        ok[l1 + 1][r1 + 1][l2 + 1][r2 + 1] = 1;
      }
    }
  }
  for (int i = 0; i < maxn; i++)
    for (int j = 0; j < maxn; j++) dp[i][j] = inf;
  dp[0][0] = 0;
  for (int pos1 = 0; pos1 < (int)s1.size(); pos1++) {
    for (int pos2 = 0; pos2 < (int)s2.size(); pos2++) {
      if (dp[pos1][pos2] == inf) continue;
      for (int len1 = 0; pos1 + len1 - 1 <= (int)s1.size(); len1++) {
        for (int len2 = 0; pos2 + len2 - 1 <= (int)s2.size(); len2++) {
          if (ok[pos1 + 1][pos1 + len1][pos2 + 1][pos2 + len2])
            dp[pos1 + len1][pos2 + len2] =
                min(dp[pos1 + len1][pos2 + len2], dp[pos1][pos2] + 1);
        }
      }
    }
  }
  if (dp[(int)s1.size()][(int)s2.size()] == inf)
    printf("-1\n");
  else
    printf("%d\n", dp[(int)s1.size()][(int)s2.size()]);
  return 0;
}
