#include <bits/stdc++.h>
using namespace std;
const int mx = 100009 << 1;
int sa[mx], lcp[mx];
char s[mx];
int ran[mx], height[mx];
int n, m;
int t2[mx], c[mx], t[mx];
void buiild_sa() {
  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 getHeight() {
  int i, j, k = 0;
  for (i = 0; i < n; i++) ran[sa[i]] = i;
  for (i = 0; i < n; i++) {
    if (k) k--;
    int j = sa[ran[i] - 1];
    while (s[i + k] == s[j + k]) k++;
    height[ran[i]] = k;
  }
}
int main() {
  while (scanf("%s", s) != EOF) {
    int l = strlen(s);
    int len1 = l;
    s[l] = 1;
    scanf("%s", s + l + 1);
    l = strlen(s);
    n = l;
    m = 200;
    buiild_sa();
    getHeight();
    int maxint = 0x3f3f3f3f;
    int k;
    for (int i = 2; i < l; i++) {
      if ((sa[i] > len1 && sa[i - 1] < len1) ||
          (sa[i] < len1 && sa[i - 1] > len1)) {
        if (height[i - 1] >= height[i] || height[i + 1] >= height[i]) continue;
        k = height[i] - 1;
        while (true) {
          if (height[i - 1] >= k || height[i + 1] >= k) break;
          k--;
        }
        if (k + 1 < maxint) maxint = k + 1;
      }
    }
    if (maxint == 0x3f3f3f3f)
      printf("-1\n");
    else
      printf("%d\n", maxint);
  }
}
