#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int N, delta;
char text[MAXN];
char v1[MAXN];
char v2[MAXN];
char hashtag[2] = {'#', '\0'};
pair<int, int> sa[MAXN];
int pos[MAXN];
int tmp[MAXN];
int lcp[MAXN];
int v1sz;
int v2sz;
bool sufCmp(pair<int, int> a, pair<int, int> b) {
  int i = a.first, j = b.first;
  if (pos[i] != pos[j]) return pos[i] < pos[j];
  i += delta;
  j += delta;
  if (i < N && j < N) return pos[i] < pos[j];
  return i > j;
}
int inFirst(int x) { return x < v1sz; }
bool diff(int x, int y) {
  return inFirst(x) && !inFirst(y) || inFirst(y) && !inFirst(x);
}
int main() {
  scanf("%s", v1);
  scanf("%s", v2);
  v1sz = strlen(v1);
  int v2sz = strlen(v2);
  strcpy(text, v1);
  strcpy(text + strlen(text), hashtag);
  strcpy(text + strlen(text), v2);
  N = strlen(text);
  for (int i = 0; i < (int)(N); ++i) sa[i] = make_pair(i, i), pos[i] = text[i];
  for (delta = 1;; delta *= 2) {
    sort(sa, sa + N, sufCmp);
    for (int i = 0; i < (int)(N - 1); ++i)
      tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]);
    for (int i = 0; i < (int)(N); ++i) pos[sa[i].first] = tmp[i];
    if (tmp[N - 1] == N - 1) break;
  }
  for (int i = 0, k = 0; i < N; i++)
    if (pos[i] != N - 1) {
      int j = sa[pos[i] + 1].first;
      while (text[j + k] == text[i + k]) k++;
      lcp[pos[i]] = k;
      if (k > 0) k--;
    }
  int answ = 0x3f3f3f3f;
  for (int i = 0; i < N - 1; ++i) {
    if (i - 1 >= 0 && lcp[i] > lcp[i - 1] && lcp[i] > lcp[i + 1] &&
        diff(sa[i].first, sa[i + 1].first))
      answ = min(answ, max(lcp[i - 1], lcp[i + 1]) + 1);
    else if (i - 1 == 0 && lcp[i] > lcp[i + 1] &&
             diff(sa[i].first, sa[i + 1].first)) {
      answ = min(answ, lcp[i + 1] + 1);
    }
  }
  if (answ == 0x3f3f3f3f)
    printf("-1\n");
  else
    printf("%d\n", answ);
  return 0;
}
