#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e3 + 42;
int t[MAXN][MAXN];
int mUA[MAXN], mUB[MAXN], mU[MAXN];
void solve(string &a, string &b, int MUL[]) {
  memset(t, 0, sizeof(t));
  for (int i = a.size() - 1; i >= 0; --i) {
    for (int j = b.size() - 1; j >= 0; --j) {
      if (a[i] == b[j]) t[i][j] = t[i + 1][j + 1] + 1;
      if (i == j) continue;
      MUL[i] = max(MUL[i], t[i][j]);
    }
    MUL[i]++;
  }
}
int main() {
  string a, b;
  cin >> a >> b;
  solve(a, a, mUA), solve(b, b, mUB), solve(a, b, mU);
  int ans = 424242;
  for (int i = 0; i < a.size(); ++i)
    for (int j = 0; j < b.size(); ++j)
      if (t[i][j] >= max(mUA[i], mUB[j])) {
        ans = min(ans, max(mUA[i], mUB[j]));
      }
  cout << ((ans == 424242) ? -1 : ans);
}
