#include <bits/stdc++.h>
using namespace std;
const long double eqs = 1e-6;
const long long max_ = 5000 + 7;
const int mod = 1000000007;
const int inf = 1e9 + 7;
const long long INF = 1e18;
inline int read() {
  int ret = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar();
  return ret * f;
}
inline void write(int x) {
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
struct sam {
  int siz, len;
  char node[max_];
  struct {
    int next[27], len, fail, cnt;
  } Tire[max_ << 1];
  sam() { Tire[0].fail = -1; }
  void buildsam() {
    len = strlen(node + 1);
    int fa = 0;
    for (int i = 1; i <= len; i++) {
      int cur = ++siz;
      Tire[cur].cnt = 1;
      Tire[cur].len = Tire[fa].len + 1;
      int now = fa;
      while (now != -1 && !Tire[now].next[node[i] - 'a']) {
        Tire[now].next[node[i] - 'a'] = cur;
        now = Tire[now].fail;
      }
      if (now == -1) {
        Tire[cur].fail = 0;
      } else {
        int to = Tire[now].next[node[i] - 'a'];
        if (Tire[now].len + 1 == Tire[to].len) {
          Tire[cur].fail = to;
        } else {
          int clone = ++siz;
          Tire[clone] = Tire[to];
          Tire[clone].cnt = 0;
          Tire[clone].len = Tire[now].len + 1;
          while (now != -1 && Tire[now].next[node[i] - 'a'] == to) {
            Tire[now].next[node[i] - 'a'] = clone;
            now = Tire[now].fail;
          }
          Tire[to].fail = Tire[cur].fail = clone;
        }
      }
      fa = cur;
    }
  }
  int sor[max_], id[max_ << 1];
  void buildendpos() {
    for (int i = 1; i <= siz; i++) sor[Tire[i].len]++;
    for (int i = 1; i <= len; i++) sor[i] += sor[i - 1];
    for (int i = siz; i >= 1; i--) id[sor[Tire[i].len]--] = i;
    for (int i = siz; i >= 1; i--) {
      Tire[Tire[id[i]].fail].cnt += Tire[id[i]].cnt;
    }
  }
} A, B;
struct kk {
  int Ap, Bp, len;
  kk() {}
  kk(int a, int b, int c) { Ap = a, Bp = b, len = c; }
};
queue<kk> que;
signed main() {
  cin >> A.node + 1;
  cin >> B.node + 1;
  A.buildsam();
  A.buildendpos();
  B.buildsam();
  B.buildendpos();
  for (int i = 0; i <= 25; i++) {
    if (A.Tire[0].next[i] && B.Tire[0].next[i]) {
      que.push(kk(A.Tire[0].next[i], B.Tire[0].next[i], 1));
    }
  }
  while (!que.empty()) {
    kk tou = que.front();
    que.pop();
    int Ap = tou.Ap, Bp = tou.Bp, len = tou.len;
    if (A.Tire[Ap].cnt == 1 && B.Tire[Bp].cnt == 1) {
      cout << len;
      exit(0);
    }
    for (int i = 0; i <= 25; i++) {
      if (A.Tire[Ap].next[i] && B.Tire[Bp].next[i]) {
        que.push(kk(A.Tire[Ap].next[i], B.Tire[Bp].next[i], len + 1));
      }
    }
  }
  cout << -1;
  return 0;
}
