#include <bits/stdc++.h>
using namespace std;
int *x = new int[10010], *y = new int[10010], cnt[10010], sa[10010];
inline int check(int i, int j, int n) {
  if ((i < n) ^ (j < n)) return false;
  return (i >= n && j >= n) || (y[i] == y[j]);
}
void calc_sa(int n, char *s) {
  int m = 256;
  for (int i = 0; i <= n - 1; i++) cnt[x[i] = s[i]]++;
  for (int i = 1; i <= m - 1; i++) cnt[i] += cnt[i - 1];
  for (int i = n - 1; i >= 0; i--) sa[--cnt[x[i]]] = i;
  for (int k = 1;; k <<= 1) {
    int p = 0;
    for (int i = n - k; i <= n - 1; i++) y[p++] = i;
    for (int i = 0; i <= n - 1; i++)
      if (sa[i] >= k) y[p++] = sa[i] - k;
    for (int i = 0; i <= m - 1; i++) cnt[i] = 0;
    for (int i = 0; i <= n - 1; i++) cnt[x[i]]++;
    for (int i = 1; i <= m - 1; i++) cnt[i] += cnt[i - 1];
    for (int i = n - 1; i >= 0; i--) sa[--cnt[x[y[i]]]] = y[i];
    swap(x, y);
    p = 1;
    x[sa[0]] = 0;
    for (int i = 1; i <= n - 1; i++)
      x[sa[i]] =
          (check(sa[i], sa[i - 1], n) && check(sa[i] + k, sa[i - 1] + k, n))
              ? p - 1
              : p++;
    m = p;
    if (m >= n) break;
  }
}
int hi[10010];
void calc_hi(int n, char *s) {
  int j, k = 0;
  for (int i = 0; i <= n - 1; i++) {
    if (!x[i]) {
      hi[x[i]] = k = 0;
      continue;
    }
    if (k) k--;
    j = sa[x[i] - 1];
    while (s[j + k] == s[i + k]) k++;
    hi[x[i]] = k;
  }
}
int n, m, ans = 1 << 30;
char s1[10010], s2[10010], s[10010];
int main() {
  scanf("%s%s", s1, s2);
  n = strlen(s1);
  m = strlen(s2);
  strcpy(s, s1);
  s[n] = '?';
  strcpy(s + n + 1, s2);
  calc_sa(n + m + 1, s);
  calc_hi(n + m + 1, s);
  for (int i = 1; i <= n + m; i++) {
    if (((sa[i] < n) ^ (sa[i - 1] < n)) && hi[i - 1] < hi[i] &&
        hi[i] > hi[i + 1]) {
      ans = min(ans, max(hi[i - 1], hi[i + 1]) + 1);
    }
  }
  if (ans > n + m)
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}
