#include <bits/stdc++.h>
using namespace std;
const int N = 55;
vector<pair<int, int> > g[26];
void func(char *s, int n, bool dp[][N][26]) {
  memset(dp, false, sizeof(dp));
  for (int i = 1; i <= n; i++) dp[i][i][s[i] - 'a'] = true;
  for (int len = 2; len <= n; len++) {
    for (int l = 1; l <= n - len + 1; l++) {
      int r = l + len - 1;
      for (int ch = 0; ch < 26; ch++) {
        for (int i = 0; i < g[ch].size(); i++) {
          int p = g[ch][i].first, q = g[ch][i].second;
          for (int k = l; k < r && !dp[l][r][ch]; k++)
            if (dp[l][k][p] && dp[k + 1][r][q]) dp[l][r][ch] = true;
        }
      }
    }
  }
}
char s1[N], s2[N];
int l1, l2;
int n;
bool f1[N][N][26], f2[N][N][26];
int dp[N][N];
int main() {
  gets(s1 + 1);
  gets(s2 + 1);
  l1 = strlen(s1 + 1);
  l2 = strlen(s2 + 1);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    char ch, ch1, ch2;
    getchar();
    scanf("%c->%c%c", &ch, &ch1, &ch2);
    g[ch - 'a'].push_back(make_pair(ch1 - 'a', ch2 - 'a'));
  }
  func(s1, l1, f1);
  func(s2, l2, f2);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 1; i <= l1; i++)
    for (int j = 1; j <= l2; j++)
      for (int ch = 0; ch < 26; ch++)
        for (int p = 0; p < i; p++)
          for (int q = 0; q < j; q++)
            if (f1[p + 1][i][ch] && f2[q + 1][j][ch] && dp[p][q] + 1 < dp[i][j])
              dp[i][j] = dp[p][q] + 1;
  if (dp[l1][l2] < 0x3f3f3f3f)
    printf("%d\n", dp[l1][l2]);
  else
    puts("-1");
  return 0;
}
