#include <bits/stdc++.h>
using namespace std;
const int N = (int)5e3 + 10;
const int B = (int)1e9 + 7;
int n, m, L[2][N], F[N][N];
char S[N], T[N];
struct SuffixArray {
  int n, SA[N], LCP[N], pos[N];
  char S[N];
  long long H[N], P[N];
  void Init(string T) {
    n = (int)T.size();
    P[0] = 1;
    H[0] = 0;
    S[n + 1] = 0;
    for (int i = 1; i <= n; ++i) {
      S[i] = T[i - 1];
      P[i] = (P[i - 1] * 31ll) % B;
      H[i] = (H[i - 1] * 31ll + S[i] - 'a') % B;
    }
    BuildSA();
    BuildLCP();
  }
  long long Hash(int l, int r) {
    return (H[r] - H[l - 1] * P[r - l + 1] + (long long)B * B) % B;
  }
  int Prefix(int x, int y) {
    int l = 1, r = min(n - x + 1, n - y + 1), f = 0;
    while (l <= r) {
      int m = (l + r) >> 1;
      if (Hash(x, x + m - 1) == Hash(y, y + m - 1)) {
        f = m;
        l = m + 1;
      } else
        r = m - 1;
    }
    return f;
  }
  void BuildSA() {
    for (int i = 1; i <= n; ++i) SA[i] = i;
    sort(SA + 1, SA + 1 + n, [this](int x, int y) {
      int f = Prefix(x, y);
      return S[x + f] < S[y + f];
    });
    for (int i = 1; i <= n; ++i) pos[SA[i]] = i;
  }
  void BuildLCP() {
    LCP[1] = 0;
    for (int i = 2; i <= n; ++i) LCP[i] = Prefix(SA[i - 1], SA[i]);
  }
} SA[2];
bool Unique(int z, int i, int len) {
  int p = SA[z].pos[i];
  if (SA[z].LCP[p] >= len || SA[z].LCP[p + 1] >= len) return false;
  return true;
}
int main() {
  scanf("%s", S + 1);
  n = strlen(S + 1);
  scanf("%s", T + 1);
  m = strlen(T + 1);
  SA[0].Init(string(S + 1, S + 1 + n));
  SA[1].Init(string(T + 1, T + 1 + m));
  for (int i = 1; i <= n; ++i) {
    L[0][i] = n + 1;
    for (int len = 1; len <= n - i + 1; ++len)
      if (Unique(0, i, len)) {
        L[0][i] = len;
        break;
      }
  }
  for (int i = 1; i <= m; ++i) {
    L[1][i] = m + 1;
    for (int len = 1; len <= m - i + 1; ++len)
      if (Unique(1, i, len)) {
        L[1][i] = len;
        break;
      }
  }
  int ans = (int)1e9;
  for (int i = n; i >= 1; --i)
    for (int j = m; j >= 1; --j) {
      F[i][j] = (S[i] == T[j]) ? (F[i + 1][j + 1] + 1) : 0;
      if (max(L[0][i], L[1][j]) <= F[i][j])
        ans = min(ans, max(L[0][i], L[1][j]));
      if (ans == 1) {
        int b = 0;
      }
    }
  if (ans == (int)1e9) ans = -1;
  printf("%d", ans);
  return 0;
}
