#include <bits/stdc++.h>
using namespace std;
const int MAXL = 10005;
const int MAXH = 15;
string str;
class SuffixArray {
 public:
  int info[2];
  int idx;
};
SuffixArray SA[MAXL];
int ID[250], sa[MAXH][MAXL], LCP[MAXL], maxh, len;
bool comp(const SuffixArray &a, const SuffixArray &b) {
  return a.info[0] == b.info[0] ? (a.info[1] < b.info[1] ? true : false)
                                : (a.info[0] < b.info[0] ? true : false);
}
void BuildSA() {
  int i, j;
  for (i = 0; i < len; i++) {
    sa[0][i] = ID[str[i]];
    SA[i].idx = 0;
    SA[i].info[0] = SA[i].info[1] = 0;
  }
  int step, jump;
  step = jump = 1;
  for (; jump <= len; jump <<= 1, step += 1) {
    for (i = 0; i < len; i++) {
      SA[i].idx = i;
      SA[i].info[0] = sa[step - 1][i];
      SA[i].info[1] = i + jump < len ? sa[step - 1][jump + i] : -1;
    }
    sort(SA, SA + len, comp);
    for (i = 0; i < len; i++) {
      sa[step][SA[i].idx] = i > 0 and SA[i].info[0] == SA[i - 1].info[0] and
                                    SA[i].info[1] == SA[i - 1].info[1]
                                ? sa[step][SA[i - 1].idx]
                                : i;
    }
  }
  maxh = step;
  return;
}
void BuildLCP() {
  int i, j, id1, id2;
  LCP[0] = 0;
  for (i = 1; i < len; i++) {
    id1 = SA[i].idx;
    id2 = SA[i - 1].idx;
    LCP[i] = 0;
    for (j = maxh - 1; j >= 0; j--) {
      if (sa[j][id1] == sa[j][id2] and sa[j][id2] != -1) {
        LCP[i] += (1 << j);
        id1 += (1 << j);
        id2 += (1 << j);
      }
    }
  }
  return;
}
void initID() {
  int i;
  ID[(int)'~'] = 0;
  for (i = ('a'); i <= ('z'); i++) ID[i] = (int)(i - 'a') + 1;
  return;
}
int main(int argc, const char *argv[]) {
  string s1, s2;
  int len1;
  while (cin >> s1 >> s2) {
    len1 = s1.length();
    str.clear();
    memset(sa, -1, sizeof(sa));
    str = s1 + "~" + s2;
    len = str.length();
    initID();
    BuildSA();
    BuildLCP();
    int res = len;
    bool ok = false;
    for (int i = 2; i < len; i++) {
      if (max(SA[i].idx, SA[i - 1].idx) > len1 and
          min(SA[i].idx, SA[i - 1].idx) < len1) {
        if (LCP[i] > LCP[i - 1] and LCP[i] > LCP[i + 1]) {
          ok = true;
          res = min(res, max(LCP[i - 1], LCP[i + 1]) + 1);
        }
      }
    }
    if (!ok)
      cout << "-1\n";
    else
      cout << res << "\n";
  }
  return 0;
}
