#include <bits/stdc++.h>
using namespace std;
const int maxn = 22000;
char s[maxn], s2[maxn];
int r[maxn], sa[maxn], rank2[maxn], height[maxn], c[maxn], x[maxn], y[maxn];
void sam(int r[], int n, int m) {
  int i, p, k;
  for (i = 0; i < m; i++) c[i] = 0;
  for (i = 0; i < n; i++) c[x[i] = r[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 (k = 1; k <= n; k <<= 1) {
    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 = 1; 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]] == y[sa[i - 1]] && y[sa[i - 1] + k] == y[sa[i] + k]
                     ? p - 1
                     : p++;
    }
    if (p >= n) break;
    m = p;
  }
}
void getheight(int r[], int n) {
  int i, j, k;
  for (i = 0; i <= n; i++) rank2[sa[i]] = i;
  k = 0;
  for (i = 0; i < n; i++) {
    if (k) k--;
    j = sa[rank2[i] - 1];
    while (r[i + k] == r[j + k]) k++;
    height[rank2[i]] = k;
  }
}
void print(char s[], int pos, int len) {
  int i;
  for (i = pos; i < len; i++) {
    printf("%c", s[i]);
  }
  printf("\n");
}
void cat(char s1[], char s2[]) {
  int len1, len2, i;
  len1 = strlen(s1);
  len2 = strlen(s2);
  s[len1] = '{';
  for (i = 0; i < len2; i++) {
    s1[i + len1 + 1] = s2[i];
  }
  s[len1 + len2 + 1] = '\0';
}
int main() {
  int i, len, j, len1, len2;
  scanf("%s%s", s, s2);
  len1 = strlen(s);
  len2 = strlen(s2);
  cat(s, s2);
  len = strlen(s);
  for (i = 0; i < len; i++) {
    r[i] = s[i] - 'a' + 1;
  }
  r[len] = 0;
  sam(r, len + 1, 30);
  getheight(r, len);
  int ans = 1e9;
  for (i = 1; i < len; i++) {
    if (height[i] > height[i - 1] && height[i] > height[i + 1]) {
      if (sa[i - 1] < len1 && sa[i] >= len1) {
        ans = min(ans, height[i]);
        ans = min(ans, max(height[i - 1], height[i + 1]) + 1);
      }
      if (sa[i] < len1 && sa[i - 1] >= len1) {
        ans = min(ans, height[i]);
        ans = min(ans, max(height[i - 1], height[i + 1]) + 1);
      }
    }
  }
  if (ans >= 1e9) ans = -1;
  printf("%d\n", ans);
  return 0;
}
