#include <bits/stdc++.h>
using namespace std;
int vis[20040];
int sa[20040], t[20040], t2[20040], c[20040];
int height[20040], Rank[20040];
inline int min(int a, int b) { return a > b ? b : a; }
inline int max(int a, int b) { return a > b ? a : b; }
void da(int *s, int n, int m) {
  int i, *x = t, *y = t2;
  for (i = 0; i < m; i++) c[i] = 0;
  for (i = 0; i < n; i++) c[x[i] = s[i]]++;
  for (i = 1; i < m; i++) c[i] += c[i - 1];
  for (i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
  for (int k = 1; k <= n; k <<= 1) {
    int p = 0;
    for (i = n - k; i < n; i++) y[p++] = i;
    for (i = 0; i < n; i++)
      if (sa[i] >= k) y[p++] = sa[i] - k;
    for (i = 0; i < m; i++) c[i] = 0;
    for (i = 0; i < n; i++) c[x[y[i]]]++;
    for (i = 0; i < m; i++) c[i] += c[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
    swap(x, y);
    p = 1;
    x[sa[0]] = 0;
    for (i = 1; i < n; i++)
      x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]
                     ? p - 1
                     : p++;
    if (p >= n) break;
    m = p;
  }
}
void Height(int *r, int n) {
  int i, j, k = 0;
  for (i = 1; i <= n; i++) Rank[sa[i]] = i;
  for (i = 0; i < n; height[Rank[i++]] = k) {
    for (k ? k-- : 0, j = sa[Rank[i] - 1]; r[i + k] == r[j + k]; k++)
      ;
  }
}
void calHeight(int *s, int n) {
  int i, j, k = 0;
  for (i = 0; i < n; i++) Rank[sa[i]] = i;
  for (i = 0; i < n; i++) {
    if (k) k--;
    int j = sa[Rank[i] - 1];
    while (s[i + k] == s[j + k]) k++;
    height[Rank[i]] = k;
  }
}
int len1 = 0;
int len2 = 0;
bool Judge(int k) {
  int a = 0, b = 0;
  for (int i = 1; i < len1 + len2 + 2; i++) {
    if (height[i] < k) {
      if (a == 1 && b == 1) return 1;
      a = b = 0;
    }
    if (sa[i] >= 0 && sa[i] < len1) a++;
    if (sa[i] > len1 && sa[i] < len1 + len2 + 2 - 1) b++;
  }
  return 0;
}
char s[1000010];
int str[1000010];
int main() {
  while (scanf("%s", s) != EOF) {
    len1 = strlen(s);
    for (int i = 0; i < len1; i++) str[i] = s[i] - 'a' + 1;
    str[len1] = 28;
    scanf("%s", s);
    len2 = strlen(s);
    for (int i = 0; i < len2; i++) str[len1 + i + 1] = s[i] - 'a' + 1;
    str[len1 + len2 + 1] = 0;
    da(str, len1 + len2 + 2, 30);
    calHeight(str, len1 + len2 + 2);
    int ans = -1;
    int r = min(len1, len2);
    for (int i = 1; i <= r; i++) {
      if (Judge(i)) {
        ans = i;
        break;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
