#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
long long read() {
  long long s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * f;
}
inline void print(long long *f, long long len) {
  for (long long i = 0; i < len; i++) printf("%lld ", f[i]);
  puts("");
}
const long long maxn = 100010;
char s[101010];
long long ans = 0;
long long b[maxn], sa[maxn], x[maxn], y[maxn], h[maxn], rk[maxn], col[maxn],
    cnt[maxn];
long long bel[maxn];
void build_SA() {
  long long num;
  for (long long i = 1; i <= n; ++i) b[x[i] = s[i]]++;
  for (long long i = 1; i <= m; ++i) b[i] += b[i - 1];
  for (long long i = n; i >= 1; --i) sa[b[x[i]]--] = i;
  for (long long k = 1; k <= n; k <<= 1) {
    num = 0;
    for (long long i = n; i > n - k; --i) y[++num] = i;
    for (long long i = 1; i <= n; ++i)
      if (sa[i] > k) y[++num] = sa[i] - k;
    for (long long i = 0; i <= m; ++i) b[i] = 0;
    for (long long i = 1; i <= n; ++i) b[x[y[i]]]++;
    for (long long i = 0; i <= m; ++i) b[i] += b[i - 1];
    for (long long i = n; i >= 1; --i) sa[b[x[y[i]]]--] = y[i];
    for (long long i = 1; i <= n; ++i) y[i] = 0;
    for (long long i = 1; i <= n; ++i) swap(x[i], y[i]);
    num = 1;
    x[sa[1]] = 1;
    for (long long i = 2; i <= n; ++i)
      x[sa[i]] = (y[sa[i]] != y[sa[i - 1]] || y[sa[i] + k] != y[sa[i - 1] + k])
                     ? ++num
                     : num;
    if (num == n) return;
    m = num;
  }
}
void build_height() {
  long long k = 0;
  for (long long i = 1; i <= n; ++i) rk[sa[i]] = i;
  for (long long i = 1; i <= n; ++i) {
    if (rk[i] == 1) continue;
    if (k) --k;
    long long j = sa[rk[i] - 1];
    while (j + k <= n && i + k <= n && s[i + k] == s[j + k]) ++k;
    h[rk[i]] = k;
  }
}
signed main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (long long i = 1, iend = n; i <= iend; ++i) bel[i] = 1;
  s[n + 1] = '@';
  scanf("%s", s + n + 1 + 1);
  n = strlen(s + 1);
  for (long long i = 1, iend = n; i <= iend; ++i) {
    if (bel[i] != 1 && s[i] != '@') bel[i] = 2;
  }
  m = 128;
  build_SA();
  build_height();
  long long ans = 10100102101;
  long long f = 0;
  for (long long i = 2, iend = n; i <= iend; ++i) {
    if (h[i] > h[i - 1] && h[i] > h[i + 1]) {
      if (bel[sa[i]] != bel[sa[i - 1]]) {
        ans = min(max(h[i - 1], h[i + 1]) + 1, ans);
        f = 1;
      }
    }
  }
  if (!f)
    puts("-1");
  else
    cout << ans << endl;
}
