#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T, class X>
inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}
struct entry {
  int nr[2], p;
} L[12009];
int P[17][12009], N, stp, cnt, rnk[12009];
string A;
int cmp(entry a, entry b) {
  return a.nr[0] == b.nr[0] ? (a.nr[1] < b.nr[1] ? 1 : 0)
                            : (a.nr[0] < b.nr[0] ? 1 : 0);
}
void buildsa() {
  N = A.size();
  for (int i = 0; i < N; i++) P[0][i] = A[i] - 'a';
  for (stp = 1, cnt = 1; cnt >> 1 < N; stp++, cnt <<= 1) {
    for (int i = 0; i < N; i++) {
      L[i].nr[0] = P[stp - 1][i];
      L[i].nr[1] = i + cnt < N ? P[stp - 1][i + cnt] : -1;
      L[i].p = i;
    }
    sort(L, L + N, cmp);
    for (int i = 0; i < N; i++)
      P[stp][L[i].p] =
          i > 0 && L[i].nr[0] == L[i - 1].nr[0] && L[i].nr[1] == L[i - 1].nr[1]
              ? P[stp][L[i - 1].p]
              : i;
  }
  for (int i = (0); i <= (N - 1); i++) rnk[P[stp - 1][i]] = i;
}
int lcp(int x, int y) {
  int k, ret = 0;
  if (x == y) return N - x;
  for (k = stp - 1; k >= 0 && x < N && y < N; k--) {
    if (P[k][x] == P[k][y]) {
      x += 1 << k;
      y += 1 << k;
      ret += 1 << k;
    }
  }
  return ret;
}
string s1, s2;
long long hs1[5004], hs2[5004], p29[5004], len1[5004], len2[5004], invp29[5004];
bool match(int a, int b) {
  int lo = max(len1[a], len2[b]);
  if (a + lo > s1.size() || b + lo > s2.size()) return 0;
  long long v1 = hs1[a + lo - 1];
  if (a != 0) {
    v1 -= hs1[a - 1];
    if (v1 < 0) v1 += 1000000007LL;
    v1 *= invp29[a];
    v1 %= 1000000007LL;
  }
  long long v2 = hs2[b + lo - 1];
  if (b != 0) {
    v2 -= hs2[b - 1];
    if (v2 < 0) v2 += 1000000007LL;
    v2 *= invp29[b];
    v2 %= 1000000007LL;
  }
  return (v1 == v2);
}
int main() {
  p29[0] = 1;
  invp29[0] = 1;
  for (int i = (1); i <= (5000); i++) {
    p29[i] = (p29[i - 1] * 29LL) % 1000000007LL;
    invp29[i] = modinverse(p29[i], 1000000007LL);
  }
  cin >> s1 >> s2;
  A = s1;
  buildsa();
  for (int i = (0); i <= (s1.size() - 1); i++) {
    hs1[i] = ((s1[i] - 'a' + 1) * p29[i]) % 1000000007LL;
    if (i > 0) hs1[i] = (hs1[i] + hs1[i - 1]) % 1000000007LL;
  }
  for (int i = (0); i <= (s1.size() - 1); i++) {
    len1[i] = 1;
    int pos = P[stp - 1][i];
    if (pos > 0) len1[i] = max(len1[i], (long long)1 + lcp(i, rnk[pos - 1]));
    if (pos < s1.size() - 1)
      len1[i] = max(len1[i], (long long)1 + lcp(i, rnk[pos + 1]));
  }
  A = s2;
  buildsa();
  for (int i = (0); i <= (s2.size() - 1); i++) {
    hs2[i] = ((s2[i] - 'a' + 1) * p29[i]) % 1000000007LL;
    if (i > 0) hs2[i] = (hs2[i] + hs2[i - 1]) % 1000000007LL;
  }
  int ans = (1 << 30) - 1;
  for (int i = (0); i <= (s2.size() - 1); i++) {
    len2[i] = 1;
    int pos = P[stp - 1][i];
    if (pos > 0) len2[i] = max(len2[i], (long long)1 + lcp(i, rnk[pos - 1]));
    if (pos < s2.size() - 1)
      len2[i] = max(len2[i], (long long)1 + lcp(i, rnk[pos + 1]));
    for (int j = (0); j <= (s1.size() - 1); j++) {
      if (match(j, i)) {
        ans = min((long long)ans, max(len1[j], len2[i]));
      }
    }
  }
  if (ans == (1 << 30) - 1) ans = -1;
  cout << ans << '\n';
  return 0;
}
