#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
const int mxn = 5006;
const int mxv = mxn * 2 * 2;
const int mxc = 26;
const int MXL = 0x3f3f3f3f;
int ans = MXL;
struct SAM {
  int e[mxv][mxc], f[mxv], len[mxv], sz, last;
  int cnt[mxv][2];
  int NEW(int tlen) {
    ++sz, f[sz] = 0, len[sz] = tlen;
    memset(e[sz], 0, sizeof e[sz]);
    return sz;
  }
  void clear() {
    sz = -1;
    NEW(-1);
    last = NEW(0);
    fill(e[0], e[0] + mxc, 1);
  }
  vi que;
  void sort() {
    static int c[mxv];
    fill(c, c + sz + 1, 0);
    for (int i = sz; i; --i) ++c[len[i]];
    partial_sum(c, c + sz + 1, c);
    que.resize(sz);
    for (int i = sz; i; --i) que[--c[len[i]]] = i;
    reverse(que.begin(), que.end());
  }
  int add(int x, char c) {
    int y = e[x][c] ? 0 : NEW(len[x] + 1);
    while (x && !e[x][c]) e[x][c] = y, x = f[x];
    if (!x)
      f[y] = 1;
    else {
      int a = e[x][c];
      if (len[x] + 1 == len[a])
        f[y] = a;
      else {
        int b = NEW(len[x] + 1);
        copy(e[a], e[a] + mxc, e[b]);
        f[b] = f[a], f[a] = f[y] = b;
        while (x && e[x][c] == a) e[x][c] = b, x = f[x];
      }
    }
    return y ? y : f[y];
  }
  void count() {
    for (auto i : que)
      if (i > 1) {
        if (cnt[i][0] == 1 && cnt[i][1] == 1) ans = min(ans, len[f[i]] + 1);
        cnt[f[i]][0] += cnt[i][0];
        cnt[f[i]][1] += cnt[i][1];
      }
  }
};
SAM A;
char s[mxn];
int main() {
  A.clear();
  for (int i = 0; i < 2; ++i) {
    scanf("%s", s);
    int x = 1;
    for (int j = 0; s[j]; ++j) {
      x = A.add(x, s[j] - 'a');
      ++A.cnt[x][i];
    }
  }
  A.sort();
  A.count();
  if (ans == MXL) ans = -1;
  cout << ans << endl;
  return 0;
}
