#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
string s1, s2;
short N, M;
short lcp[MAXN][MAXN];
short minu1[MAXN], minu2[MAXN];
int ans = MAXN;
void getlcp(string &s, string &t) {
  memset(lcp, 0, sizeof(lcp));
  short s1 = s.length(), s2 = t.length();
  for (short i = s1 - 1; i >= 0; i--) {
    for (short j = s2 - 1; j >= 0; j--) {
      lcp[i][j] = (s[i] == t[j]) ? lcp[i + 1][j + 1] + 1 : 0;
    }
  }
}
void getminu(string &s, short minu[MAXN]) {
  short len = s.length();
  for (short i = 0; i < len; i++) {
    short &ref = minu[i];
    for (short j = 0; j < len; j++) {
      if (i != j) {
        if (ref < lcp[i][j]) {
          ref = lcp[i][j];
        }
      }
    }
    ref++;
  }
}
int main() {
  cin >> s1 >> s2;
  N = s1.length();
  M = s2.length();
  getlcp(s1, s1);
  getminu(s1, minu1);
  getlcp(s2, s2);
  getminu(s2, minu2);
  getlcp(s1, s2);
  for (short i = 0; i < N; i++) {
    for (short j = 0; j < M; j++) {
      short mu = max(minu1[i], minu2[j]);
      if (lcp[i][j] >= mu) {
        if (ans > mu) {
          ans = mu;
        }
      }
    }
  }
  cout << (ans == MAXN ? -1 : ans);
}
