#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
char a[N];
int b[N], c[N], d[N], e[N], sa[N], height[N], n, m, bg[N];
int rmq[N][22];
char p[N], ss[N];
int cmp(int *r, int a, int b, int l) {
  return r[a] == r[b] && r[a + l] == r[b + l];
}
void da() {
  int i, j, p, *x = c, *y = d, *t;
  for (i = 0; i < m; i++) b[i] = 0;
  for (i = 0; i < n; i++) b[x[i] = a[i]]++;
  for (i = 1; i < m; i++) b[i] += b[i - 1];
  for (i = n - 1; i >= 0; i--) sa[--b[x[i]]] = i;
  for (j = 1, p = 1; p < n; j *= 2, m = p) {
    for (p = 0, i = n - j; i < n; i++) y[p++] = i;
    for (i = 0; i < n; i++)
      if (sa[i] >= j) y[p++] = sa[i] - j;
    for (i = 0; i < n; i++) e[i] = x[y[i]];
    for (i = 0; i < m; i++) b[i] = 0;
    for (i = 0; i < n; i++) b[e[i]]++;
    for (i = 1; i < m; i++) b[i] += b[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--b[e[i]]] = y[i];
    for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
      x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
  }
}
void callheight() {
  int i, j, k = 0;
  for (i = 1; i < n; i++) b[sa[i]] = i;
  n--;
  for (i = 0; i < n; height[b[i++]] = k)
    for (k ? k-- : 0, j = sa[b[i] - 1]; a[i + k] == a[j + k]; k++)
      ;
}
void initrmq(int n) {
  int i, k;
  for (i = 2; i <= n; ++i) rmq[i][0] = height[i];
  for (k = 1; (1 << k) <= n; ++k) {
    for (i = 2; i + (1 << k) - 1 <= n; ++i) {
      rmq[i][k] = min(rmq[i][k - 1], rmq[i + (1 << (k - 1))][k - 1]);
    }
  }
}
int lcp(int aa, int bb) {
  aa = b[aa], bb = b[bb];
  if (aa > bb) swap(aa, bb);
  ++aa;
  int k = (int)(log((bb - aa + 1) * 1.0) / log(2.0));
  return min(rmq[aa][k], rmq[bb - (1 << k) + 1][k]);
}
void print() {
  int i;
  cout << "length : " << n << endl;
  cout << "s[n] = '$' " << endl;
  cout << "sorted suffix index(0 based) : " << endl;
  for (int i = 0; i <= n; i++) cout << sa[i] << " , ";
  cout << endl;
  cout << "rank of suffix " << endl;
  for (int i = 0; i <= n; i++) cout << b[i] << " , ";
  cout << endl;
  cout << "LCP between i and i-1 " << endl;
  for (int i = 0; i <= n; i++) cout << height[i] << " , ";
  cout << endl;
}
char s[2005];
int lst, md;
int main() {
  int i, j, k, q;
  scanf("%s", s);
  strcat(a, s);
  strcat(a, "}");
  md = strlen(a);
  scanf("%s", s);
  strcat(a, s);
  strcat(a, "$");
  n = strlen(a);
  lst = n;
  m = 128;
  da();
  callheight();
  initrmq(n);
  int ans = n + 5;
  for (i = 1; i <= n + 1; i++) {
    int x = sa[i - 1];
    int y = sa[i];
    int z = height[i];
    if (y < x) swap(x, y);
    if (z && (x < md - 1 && y >= md)) {
      if (height[i - 1] < z && height[i + 1] < z) {
        int xx = max(height[i - 1], height[i + 1]);
        if (z >= xx + 1) ans = min(ans, xx + 1);
      }
    }
  }
  if (ans == n + 5) ans = -1;
  printf("%d\n", ans);
  return 0;
}
