#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxpq = priority_queue<T>;
inline long long CC(long long n, long long y) {
  return (((n) + (y - 1) - 1) / (y - 1)) - (((n) + (y)-1) / (y));
}
inline long long FF(long long n, long long y) { return n / y - n / (y + 1); }
inline long long __gcd(long long a, long long b) {
  return !b ? a : __gcd(b, a % b);
}
inline int add(int a, int b, int p = 1000000007) {
  int c = a + b;
  if (c >= p) c -= p;
  return c;
}
inline int sub(int a, int b, int p = 1000000007) {
  int c = a - b;
  if (c < 0) c += p;
  return c;
}
inline int mul(int a, int b, int p = 1000000007) { return (a * 1ll * b) % p; }
int adj[55][55];
int dp[55][55];
int mask0[55][55], mask1[55][55];
string s, t;
int n, m;
const int inf = 1e9;
inline int d(char c) { return c - 'a'; }
inline bool bit(int i, int b) { return i & (1 << b); }
void solve(int mask[55][55], string s) {
  int n = s.size();
  for (int i = (n - 1); i > (-1); i--) {
    for (int j = (i); j < (n); j++) {
      if (i == j) {
        mask[i][j] = 1 << d(s[i]);
        42;
      } else {
        for (int t = (i); t < (j); t++) {
          for (int b1 = (0); b1 < (26); b1++) {
            if (!bit(mask[i][t], b1)) continue;
            for (int b2 = (0); b2 < (26); b2++) {
              if (!bit(mask[t + 1][j], b2)) continue;
              mask[i][j] |= adj[b1][b2];
            }
          }
        }
      }
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  clock_t clk = clock();
  cerr << "I will return...\n";
  cin >> s >> t;
  n = s.size(), m = t.size();
  int k;
  cin >> k;
  for (int i = (0); i < (k); i++) {
    string s;
    cin >> s;
    adj[d(s[3])][d(s[4])] |= 1 << d(s[0]);
  }
  solve(mask0, s);
  solve(mask1, t);
  for (int i = (0); i < (n + 1); i++) {
    for (int j = (0); j < (m + 1); j++) {
      dp[i][j] = inf;
      if (i == 0 && j == 0) dp[i][j] = 0;
      for (int k = (0); k < (i); k++) {
        for (int l = (0); l < (j); l++) {
          42;
          if (mask0[k][i - 1] & mask1[l][j - 1]) {
            dp[i][j] = min(dp[i][j], dp[k][l] + 1);
            42;
          }
        }
      }
      42;
    }
  }
  cout << (dp[n][m] == inf ? -1 : dp[n][m]) << '\n';
  cerr << "...don't you ever hang your head.\n";
  cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC
       << '\n';
}
