#include <bits/stdc++.h>
using namespace std;
const int MAX = 5005;
const int MOD = 1e9 + 7;
int base = 13331;
unsigned long long H1[MAX], H2[MAX], save[MAX];
void pre() {
  save[0] = 1;
  for (int i = 1; i < MAX; i++) save[i] = (save[i - 1] * base) % MOD;
}
void setHash1(string &s) {
  H1[0] = 1;
  int n = s.size();
  for (int i = 1; i < n; i++) H1[i] = (H1[i - 1] * base + s[i]) % MOD;
}
int getHash1(int l, int r) {
  return (H1[r] - (save[r - l + 1] * H1[l - 1]) % MOD + MOD) % MOD;
}
void setHash2(string &s) {
  H2[0] = 1;
  int n = s.size();
  for (int i = 1; i < n; i++) H2[i] = (H2[i - 1] * base + s[i]) % MOD;
}
int getHash2(int l, int r) {
  return (H2[r] - (save[r - l + 1] * H2[l - 1]) % MOD + MOD) % MOD;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s1, s2;
  cin >> s1 >> s2;
  pre();
  s1 = "#" + s1;
  s2 = "#" + s2;
  if (s1.size() > s2.size()) swap(s1, s2);
  setHash1(s1);
  setHash2(s2);
  int n = s1.size(), m = s2.size(), mxN = 0;
  int st = 1, en = min(n, m) - 1, mid;
  while (st <= en) {
    mid = (st + en) >> 1;
    unordered_map<int, int> mp, mp1;
    for (int j = mid; j < n; j++) mp[getHash1(j - mid + 1, j)]++;
    bool flag = 0;
    for (int j = mid; j < m; j++) {
      if (mp[getHash2(j - mid + 1, j)]) {
        mxN = mid;
        st = mid + 1;
        flag = 1;
        break;
      }
      mp1[getHash2(j - mid + 1, j)]++;
    }
    if (!flag) en = mid - 1;
  }
  for (int i = 1; i <= mxN; i++) {
    unordered_map<int, int> mp;
    vector<int> v;
    for (int j = i; j < n; j++) mp[getHash1(j - i + 1, j)]++;
    for (auto &j : mp) {
      if (j.second != 1)
        j.second = 0;
      else
        v.push_back(j.first);
    }
    for (int j = i; j < m; j++) {
      if (mp[getHash2(j - i + 1, j)] == 0) continue;
      mp[getHash2(j - i + 1, j)]++;
    }
    for (auto j : v) {
      if (mp[j] == 2) {
        cout << i << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
}
