#include <bits/stdc++.h>
using namespace std;
int a[51], b[51], l1, l2;
int n;
int oka[51][51], okb[51][51];
int c[51][3];
int f[51][51];
int main() {
  int i, j, k, l;
  char s[51];
  scanf("%s", s);
  l1 = strlen(s);
  for (i = 0; i < l1; i++) a[i + 1] = s[i] - 'a';
  scanf("%s", s);
  l2 = strlen(s);
  for (i = 0; i < l2; i++) b[i + 1] = s[i] - 'a';
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%s", s);
    c[i][0] = s[0] - 'a';
    c[i][1] = s[3] - 'a';
    c[i][2] = s[4] - 'a';
  }
  for (i = 1; i <= l1; i++) oka[i][i] = 1 << a[i];
  for (i = 2; i <= l1; i++)
    for (j = 1; j + i - 1 <= l1; j++)
      for (k = j; k < j + i - 1; k++)
        for (l = 1; l <= n; l++)
          if ((oka[j][k] & (1 << c[l][1])) &&
              (oka[k + 1][j + i - 1] & (1 << c[l][2])))
            oka[j][j + i - 1] |= 1 << c[l][0];
  for (i = 1; i <= l2; i++) okb[i][i] = 1 << b[i];
  for (i = 2; i <= l2; i++)
    for (j = 1; j + i - 1 <= l2; j++)
      for (k = j; k < j + i - 1; k++)
        for (l = 1; l <= n; l++)
          if ((okb[j][k] & (1 << c[l][1])) &&
              (okb[k + 1][j + i - 1] & (1 << c[l][2])))
            okb[j][j + i - 1] |= 1 << c[l][0];
  memset(f, 0b01111111, sizeof(f));
  f[0][0] = 0;
  for (i = 1; i <= l1; i++)
    for (j = 1; j <= l2; j++)
      for (k = 0; k < i; k++)
        for (l = 0; l < j; l++)
          if ((oka[k + 1][i] & okb[l + 1][j]) && f[i][j] > f[k][l] + 1)
            f[i][j] = f[k][l] + 1;
  if (f[l1][l2] != 2139062143)
    printf("%d\n", f[l1][l2]);
  else
    printf("-1\n");
  return 0;
}
