#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005;
struct Sam {
  int next[maxn << 2][27];
  int link[maxn << 2], step[maxn << 2];
  int a[maxn << 2], b[maxn << 2];
  int endpos[maxn << 2], mark[maxn << 2];
  int sz, last, len;
  char s[maxn << 1];
  void add(int c, int m) {
    int p = last;
    int np = last = ++sz;
    endpos[np] = 1;
    mark[np] = m;
    step[np] = step[p] + 1;
    while (!next[p][c] && p) {
      next[p][c] = np;
      p = link[p];
    }
    if (p == 0) {
      link[np] = 1;
    } else {
      int q = next[p][c];
      if (step[p] + 1 == step[q]) {
        link[np] = q;
      } else {
        int nq = ++sz;
        memcpy(next[nq], next[q], sizeof(next[q]));
        step[nq] = step[p] + 1;
        link[nq] = link[q];
        link[q] = link[np] = nq;
        while (next[p][c] == q && p) {
          next[p][c] = nq;
          p = link[p];
        }
      }
    }
  }
  void build() {
    sz = last = 1;
    scanf("%s", s);
    len = strlen(s);
    for (int i = 0; i < len; i++) {
      add(s[i] - 'a', 1);
    }
    s[len] = '{';
    scanf("%s", s + len + 1);
    len += strlen(s + len);
    for (int i = step[last]; i < len; i++) {
      add(s[i] - 'a', 2);
    }
    for (int i = 1; i <= sz; i++) {
      ++a[step[i]];
    }
    for (int i = 1; i <= len; i++) {
      a[i] += a[i - 1];
    }
    for (int i = 1; i <= sz; i++) {
      b[a[step[i]]--] = i;
    }
  }
  void solve() {
    build();
    int ans = 6000;
    for (int i = sz; i > 1; i--) {
      int e = b[i];
      if (mark[e] == 3 && endpos[e] == 2) {
        ans = min(ans, step[link[e]] + 1);
      }
      mark[link[e]] |= mark[e];
      endpos[link[e]] += endpos[e];
    }
    printf("%d\n", ans == 6000 ? -1 : ans);
  }
} sam;
int main() {
  sam.solve();
  return 0;
}
