#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int maxn = 100005;
string s;
int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
void build_sa(int m) {
  int *x = t, *y = t2;
  for (int i = 0; i < m; i++) c[i] = 0;
  for (int i = 0; i < n; i++) c[x[i] = s[i]]++;
  for (int i = 1; i < m; i++) c[i] += c[i - 1];
  for (int i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
  for (int k = 1; k <= n; k <<= 1) {
    int p = 0;
    for (int i = n - k; i < n; i++) y[p++] = i;
    for (int i = 0; i < n; i++)
      if (sa[i] >= k) y[p++] = sa[i] - k;
    for (int i = 0; i < m; i++) c[i] = 0;
    for (int i = 0; i < n; i++) c[x[y[i]]]++;
    for (int i = 0; i < m; i++) c[i] += c[i - 1];
    for (int i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
    swap(x, y);
    p = 1;
    x[sa[0]] = 0;
    for (int 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;
  }
}
int _rank[maxn], height[maxn];
void get_Height() {
  for (int i = 0; i < n; i++) _rank[sa[i]] = i;
  int k = 0;
  for (int 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;
  }
}
set<int> d;
int cnt[10005];
int main() {
  string a, b;
  cin >> a >> b;
  int sz1 = a.size(), sz2 = b.size();
  s = a + '$' + b + '#';
  n = s.size();
  s.resize(maxn);
  build_sa('z' + 1);
  get_Height();
  int cmp = *min_element(height + 1, height + n);
  int f = 0;
  int pos = 1;
  int len = ~0u >> 1;
  for (int i = 1; i < n; i++) {
    if (sa[i] >= 0 && sa[i] < sz1 && sa[i - 1] >= sz1 &&
        sa[i - 1] < sz1 + sz2 + 1) {
      f = 1;
      int m = 0;
      m = max(height[i - 1], m);
      m = max(height[i + 1], m);
      if (m + 1 <= height[i]) len = min(len, m + 1);
    } else if (sa[i - 1] >= 0 && sa[i - 1] < sz1 && sa[i] > sz1 &&
               sa[i] < sz1 + sz2 + 1) {
      f = 1;
      int m = 0;
      m = max(height[i - 1], m);
      m = max(height[i + 1], m);
      if (m + 1 <= height[i]) len = min(len, m + 1);
    }
  }
  if (!f)
    cout << -1 << endl;
  else {
    if (len != (~0u >> 1))
      cout << len << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}
