#include <bits/stdc++.h>
using namespace std;
struct tri {
  int atual, custo;
  tri(int atual = 0, int custo = 0) : atual(atual), custo(custo) {}
};
struct aresta {
  int atual;
  string str;
  aresta(int atual = 0, string str = "") : atual(atual), str(str) {}
};
int x[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int y[] = {-1, 0, 1, 1, 1, 0, -1, -1};
inline void read(int &n) {
  n = 0;
  register bool neg = false;
  register char c = getchar_unlocked();
  if (c == EOF) {
    n = -1;
    return;
  }
  while (!('0' <= c && c <= '9')) {
    if (c == '-') neg = true;
    c = getchar_unlocked();
  }
  while ('0' <= c && c <= '9') {
    n = n * 10 + c - '0';
    c = getchar_unlocked();
  }
  n = (neg ? (-n) : (n));
}
inline void writeInt(int n) {
  register int idx = 20;
  if (n < 0) putchar_unlocked('-');
  n = abs(n);
  char out[21];
  out[20] = ' ';
  do {
    idx--;
    out[idx] = n % 10 + '0';
    n /= 10;
  } while (n);
  do {
    putchar_unlocked(out[idx++]);
  } while (out[idx] != ' ');
}
char str1[5000603], str2[5000603], texto[5000603];
int RA[5000603], SA[5000603], LCP[5000603], *FC, *SC, step;
bool cmp(int a, int b) {
  if (step == -1 || FC[a] != FC[b]) return FC[a] < FC[b];
  return FC[a + (1 << step)] < FC[b + (1 << step)];
}
void suffix_array(char *S, int n) {
  for (int i = 0; i < n; ++i) RA[i] = S[SA[i] = i];
  for (FC = RA, SC = LCP, step = -1; (1 << step) < n; step++) {
    sort(SA, SA + n, cmp);
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if (i > 0 && cmp(SA[i - 1], SA[i])) cnt++;
      SC[SA[i]] = cnt;
    }
    if (cnt == n - 1) break;
    swap(FC, SC);
  }
}
int ranking[5000603];
int lcp[5000603];
int idx[5000603];
void compute_lcp(int n) {
  int i, j, h = 0;
  for (i = 0; i != n; ++i) {
    ranking[SA[i]] = i;
  }
  for (i = 0; i != n; ++i)
    if (ranking[i] > 0) {
      j = SA[ranking[i] - 1];
      while (texto[i + h] != 'A' && texto[i + h] != '.' &&
             texto[i + h] == texto[j + h]) {
        h++;
      }
      lcp[ranking[i]] = h;
      if (h > 0) {
        h--;
      }
    }
}
int main() {
  ios::sync_with_stdio(false);
  int j = 0;
  char c = getchar_unlocked();
  while (c != '\n') {
    idx[j] = 1;
    texto[j++] = c;
    c = getchar_unlocked();
  }
  idx[j] = 3;
  texto[j++] = '.';
  c = getchar_unlocked();
  while (c != '\n' && c != EOF) {
    texto[j] = c;
    idx[j++] = 2;
    c = getchar_unlocked();
  }
  texto[j] = '\0';
  suffix_array(texto, j);
  compute_lcp(j);
  int lo = 0, hi = j;
  int resp = 0, idd = 99999999;
  for (int mid = 1; mid <= j; ++mid) {
    int id = 99999999;
    for (int i = 0; i < j; ++i) {
      if (lcp[i] > 0 && lcp[i] >= mid) {
        int at = 99999999;
        int id1 = 0;
        int id2 = 0;
        if (idx[SA[i]] == 1) id1++;
        if (idx[SA[i - 1]] == 1) id1++;
        if (((idx[SA[i]] == 2) || (idx[SA[i - 1]] == 2))) {
          if (idx[SA[i]] == 2) {
            id2++;
            at = min(at, SA[i]);
          }
          if (idx[SA[i - 1]] == 2) {
            id2++;
            at = min(at, SA[i - 1]);
          }
        }
        int k = i + 1;
        while (k < j && lcp[k] >= mid) {
          if (idx[SA[k]] == 1) id1++;
          if (idx[SA[k]] == 2) id2++;
          at = min(at, SA[k]);
          k++;
        }
        if (id1 == 1 && id2 == 1) id = min(id, at);
        i = k - 1;
      }
    }
    if (id != 99999999) {
      resp = mid;
      idd = id;
      break;
    } else
      hi = mid - 1;
  }
  if (idd != 99999999 && resp > 0) {
    printf("%d\n", resp);
  } else
    printf("-1\n");
  return 0;
}
