#include <bits/stdc++.h>
using namespace std;
int nextint() {
  int _;
  scanf("%d", &_);
  return _;
}
int main() {
  string s1, s2;
  cin >> s1 >> s2;
  string m1 = " ", m2 = " ";
  m1[0] = 'z' + 1;
  m2[0] = 'z' + 2;
  string s = s1 + m1 + s2 + m2;
  vector<int> p(s.size()), pn(s.size());
  vector<int> c(s.size()), cn(s.size());
  vector<int> cnt(2 * 5555);
  for (int i = 0; i < s.size(); i++) cnt[s[i]]++;
  for (int i = 1; i < cnt.size(); i++) cnt[i] += cnt[i - 1];
  for (int i = 0; i < s.size(); i++) p[--cnt[s[i]]] = i;
  int classes = 0;
  c[p[0]] = 0;
  for (int i = 1; i < s.size(); i++) {
    if (s[p[i]] != s[p[i - 1]]) classes++;
    c[p[i]] = classes;
  }
  vector<vector<int> > cs;
  cs.push_back(c);
  int n = s.size();
  for (int q = 0; (1 << q) < n; q++) {
    for (int i = 0; i < p.size(); i++) {
      pn[i] = p[i] - (1 << q);
      if (pn[i] < 0) pn[i] += n;
    }
    fill(cnt.begin(), cnt.end(), 0);
    for (int i = 0; i < p.size(); i++) cnt[c[pn[i]]]++;
    for (int i = 1; i < cnt.size(); i++) cnt[i] += cnt[i - 1];
    for (int i = s.size() - 1; i >= 0; i--) p[--cnt[c[pn[i]]]] = pn[i];
    int classes = 0;
    cn[p[0]] = 0;
    for (int i = 1; i < c.size(); i++) {
      int ci = (p[i] + (1 << q)) % n;
      int pi = (p[i - 1] + (1 << q)) % n;
      if (c[p[i]] != c[p[i - 1]] || c[ci] != c[pi]) classes++;
      cn[p[i]] = classes;
    }
    c = cn;
    cs.push_back(c);
  }
  int logn = 0;
  vector<int> lcm(s.size() - 1);
  for (int i = 0; i < lcm.size(); i++) {
    int pi = p[i];
    int pj = p[i + 1];
    for (int h = cs.size() - 1; h >= 0; h--) {
      if (pi + (1 << h) <= n && pj + (1 << h) <= n && cs[h][pi] == cs[h][pj]) {
        lcm[i] += (1 << h);
        pi += (1 << h);
        pj += (1 << h);
      }
    }
  }
  int res = 1 << 30;
  for (int i = 0; i < lcm.size(); i++) {
    int prv = 0;
    if (i > 0) prv = lcm[i - 1];
    int nxt = 0;
    if (i + 1 < lcm.size()) nxt = lcm[i + 1];
    if (lcm[i] > prv && lcm[i] > nxt)
      if ((p[i] < s1.size()) != (p[i + 1] < s1.size()))
        res = min(res, max(prv + 1, nxt + 1));
  }
  if (res == (1 << 30)) res = -1;
  cout << res << endl;
  return 0;
}
