#include <bits/stdc++.h>
#pragma comment(linker, "/stack:16777216")
using namespace std;
template <class T>
void out(map<char, T>& a, string s = "(%3c ->%3d)") {
  for (auto(it) = (a).begin(); (it) != (a).end(); ++(it))
    printf(s.c_str(), it->first, it->second);
  printf("\n");
}
template <class T>
void out(unordered_map<char, T>& a, string s = "(%3c ->%3d)") {
  for (auto(it) = (a).begin(); (it) != (a).end(); ++(it))
    printf(s.c_str(), it->first, it->second);
  printf("\n");
}
template <class T>
void out(vector<T>& a, string s = "%3d ") {
  int i, n = a.size();
  for ((i) = 0; (i) < (n); (i)++) printf(s.c_str(), a[i]);
  printf("\n");
}
template <class T>
void out(T* a, int n, string s = "%3d ") {
  int i;
  for ((i) = 0; (i) < (n); (i)++) printf(s.c_str(), a[i]);
  printf("\n");
}
long long i, j, N, M, n, m, k, p;
unordered_map<char, int> nxt[21000];
vector<int> rev_next[21000];
int len[21000], link[21000];
int sz, last, cur;
vector<int> by_len[21000];
vector<int> rev_link[21000];
char ch[21000];
int f_cnt[21000];
int cnt[21000];
void init_suff() {
  sz = 0;
  last = cur = sz++;
  len[last] = 0;
  link[last] = -1;
  by_len[len[last]].push_back(last);
  f_cnt[last] = cnt[last] = 0;
  ch[last] = '!';
}
void add_suff(char c) {
  int p, q, clone;
  cur = sz++;
  len[cur] = len[last] + 1;
  by_len[len[cur]].push_back(cur);
  f_cnt[cur] = cnt[cur] = 1;
  ch[cur] = c;
  for (p = last; p != -1 && nxt[p].count(c) == 0; p = link[p]) nxt[p][c] = cur;
  if (p == -1)
    link[cur] = 0;
  else {
    q = nxt[p][c];
    if (len[q] == len[p] + 1)
      link[cur] = q;
    else {
      clone = sz++;
      nxt[clone] = nxt[q];
      len[clone] = len[p] + 1;
      by_len[len[clone]].push_back(clone);
      link[clone] = link[q];
      f_cnt[clone] = cnt[clone] = 0;
      ch[clone] = ch[q];
      for (; p != -1 && nxt[p][c] == q; p = link[p]) nxt[p][c] = clone;
      link[q] = link[cur] = clone;
    }
  }
  last = cur;
}
int d[21000];
int dfs(int v) {
  if (d[v] != -1) return d[v];
  d[v] = 1;
  for (auto(it) = (nxt[v]).begin(); (it) != (nxt[v]).end(); ++(it))
    d[v] += dfs(it->second);
  return d[v];
}
int mask[21000];
int dfs_mask(int v) {
  if (mask[v] != -1) return mask[v];
  mask[v] = 0;
  if (ch[v] == '#') return mask[v] = 1;
  if (ch[v] == '+') return mask[v] = 2;
  for (auto(it) = (nxt[v]).begin(); (it) != (nxt[v]).end(); ++(it))
    mask[v] |= dfs_mask(it->second);
  return mask[v];
}
int sub_len[21000];
int MainSolve(string args = "") {
  cout << args;
  string s1, s2;
  cin >> s1 >> s2;
  swap(s1, s2);
  string s = "$" + s1 + "#" + s2 + "+";
  init_suff();
  int f_last = -1;
  for ((i) = 0; (i) < ((s).length()); (i)++) {
    add_suff(s[i]);
    if (s[i] == '#') {
      for (int u = sz; u-- > 0;) {
        for (auto el : by_len[u])
          if (link[el] != -1) f_cnt[link[el]] += f_cnt[el];
      }
      f_last = sz;
    }
  }
  for ((i) = 0; (i) < (sz); (i)++) d[i] = -1;
  for ((i) = 0; (i) < (sz); (i)++) dfs(sz - i - 1);
  for ((i) = 0; (i) < (sz); (i)++) mask[i] = -1;
  for ((i) = 0; (i) < (sz); (i)++) dfs_mask(sz - i - 1);
  for ((i) = 0; (i) < (sz); (i)++) sub_len[i] = -1;
  queue<int> q;
  q.push(0);
  sub_len[0] = 0;
  while (q.size()) {
    auto v = q.front();
    q.pop();
    for (auto el : nxt[v])
      if (mask[el.second] == 3 && sub_len[el.second] == -1) {
        q.push(el.second);
        sub_len[el.second] = sub_len[v] + 1;
      }
  }
  for ((i) = 0; (i) < (sz); (i)++)
    if (i) rev_link[link[i]].push_back(i);
  for (i = sz; i-- > 0;) {
    for (auto el : by_len[i])
      if (link[el] != -1) cnt[link[el]] += cnt[el];
  }
  for ((i) = 0; (i) < (sz); (i)++)
    for (auto el : nxt[i]) rev_next[el.second].push_back(i);
  int separator = -1;
  for ((i) = 0; (i) < (sz); (i)++)
    if (ch[i] == '#') {
      separator = i;
      break;
    }
  int mni = -1;
  for ((i) = 0; (i) < (sz); (i)++)
    if (mask[i] == 3 && (mni == -1 || sub_len[mni] > sub_len[i]) &&
        cnt[i] == 2 && (i >= f_last || f_cnt[i] == 1))
      mni = i;
  string mytry = "";
  if (mni != -1)
    for (i = mni; i != 0;) {
      mytry.push_back(ch[i]);
      for (auto el : rev_next[i])
        if (mask[el] == 3) {
          i = el;
          break;
        }
    }
  if (mni != -1)
    cout << sub_len[mni] << endl;
  else
    cout << -1 << endl;
  return 0;
}
int main() {
  MainSolve();
  return 0;
}
