#include <bits/stdc++.h>
using namespace std;
const int iinf = 0x3f3f3f3f;
const long long linf = 0x3f3f3f3f3f3f3f3f;
const int N = 5000;
int an, bn, ans = iinf;
string a, b;
const int tN = (N << 1) + 1;
struct sam {
  int tn, ch[tN][26], fa[tN], len[tN], rt, t;
  int sz[tN], cnt[N + 1], q[tN];
  int newn() {
    fill(ch[tn], ch[tn] + 26, -1);
    fa[tn] = -1, sz[tn] = 0;
    return tn++;
  }
  sam() { tn = 0, rt = t = newn(); }
  void insert(int c) {
    int p = t, np = t = newn();
    len[np] = len[p] + 1;
    for (; ~p && !~ch[p][c]; p = fa[p]) ch[p][c] = np;
    if (!~p)
      fa[np] = rt;
    else {
      int q = ch[p][c];
      if (len[q] == len[p] + 1)
        fa[np] = q;
      else {
        int nq = newn();
        copy(ch[q], ch[q] + 26, ch[nq]);
        len[nq] = len[p] + 1, fa[nq] = fa[q], fa[q] = fa[np] = nq;
        for (; ~p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
      }
    }
    sz[np] = 1;
  }
  void build() {
    fill(cnt, cnt + N + 1, 0);
    for (int i = (0), I = (tn); i < I; i++) cnt[len[i]]++;
    for (int i = (1), I = (N + 1); i < I; i++) cnt[i] += cnt[i - 1];
    for (int i = (0), I = (tn); i < I; i++) q[--cnt[len[i]]] = i;
    for (int i = (tn)-1, I = (0) - 1; i > I; i--)
      if (i != rt) sz[fa[q[i]]] += sz[q[i]];
  }
} as, bs;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> a >> b, an = int((a).size()), bn = int((b).size());
  for (int i = (0), I = (an); i < I; i++) as.insert(a[i] - 'a');
  for (int i = (0), I = (bn); i < I; i++) bs.insert(b[i] - 'a');
  as.build(), bs.build();
  for (int l = (0), I = (an); l < I; l++) {
    int pa = as.rt, pb = bs.rt;
    for (int r = (l), I = (an); r < I; r++) {
      pa = as.ch[pa][a[r] - 'a'];
      pb = bs.ch[pb][a[r] - 'a'];
      if (!~pb) break;
      if (as.sz[pa] == 1 && bs.sz[pb] == 1) ans = min(ans, r - l + 1);
    }
  }
  if (ans == iinf)
    cout << -1 << '\n';
  else
    cout << ans << '\n';
  return 0;
}
