#include <bits/stdc++.h>
using namespace std;
const int INF = 1024;
const int MAXN = 52;
const int SIGMA = 27;
vector<pair<int, int> > v[SIGMA];
void gao(int n, char s[MAXN], set<int> m[MAXN][SIGMA]) {
  for (int k = n - 1; k >= 0; --k) {
    queue<pair<int, int> > q;
    m[k][s[k] - 'a'].insert(k + 1);
    q.push(make_pair(s[k] - 'a', k + 1));
    while (!q.empty()) {
      int i = q.front().first;
      int j = q.front().second;
      q.pop();
      for (vector<pair<int, int> >::const_iterator it = v[i].begin();
           it != v[i].end(); ++it) {
        for (set<int>::const_iterator jt = m[j][it->first].begin();
             jt != m[j][it->first].end(); ++jt) {
          if (m[k][it->second].insert(*jt).second) {
            q.push(make_pair(it->second, *jt));
          }
        }
      }
    }
  }
}
set<int> ma[MAXN][SIGMA], mb[MAXN][SIGMA];
int mind[MAXN][MAXN];
int main() {
  int na, nb, n;
  char sa[MAXN], sb[MAXN], s[MAXN];
  scanf("%s%s", sa, sb);
  na = strlen(sa);
  nb = strlen(sb);
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%s", s);
    v[s[3] - 'a'].push_back(make_pair(s[4] - 'a', s[0] - 'a'));
  }
  gao(na, sa, ma);
  gao(nb, sb, mb);
  fill(mind[0], mind[na + 1], INF);
  mind[0][0] = 0;
  for (int i = 0; i < na; ++i) {
    for (int j = 0; j < nb; ++j) {
      if (mind[i][j] == INF) {
        continue;
      }
      for (int k = 0; k < SIGMA; ++k) {
        for (set<int>::const_iterator it = ma[i][k].begin();
             it != ma[i][k].end(); ++it) {
          for (set<int>::const_iterator jt = mb[j][k].begin();
               jt != mb[j][k].end(); ++jt) {
            mind[*it][*jt] = min(mind[*it][*jt], mind[i][j] + 1);
          }
        }
      }
    }
  }
  printf("%d\n", mind[na][nb] == INF ? -1 : mind[na][nb]);
  return 0;
}
