#include <bits/stdc++.h>
using namespace std;
const int ALPH = 28;
const char SC = 'a';
const int MAXN = (int)(1e5 + 100);
struct node {
  int par;
  int l, r, id;
  int next[ALPH];
  int suflink;
  int res_cnt[2];
};
node t[2 * MAXN];
int tsz = 0;
char s[2][MAXN];
int slen[2];
inline int new_node(int par, int l, int r, int id) {
  node &v = t[tsz];
  v.par = par;
  v.l = l;
  v.r = r;
  v.id = id;
  memset(v.next, -1, sizeof(v.next));
  v.suflink = -1;
  memset(v.res_cnt, 0, sizeof(v.res_cnt));
  return tsz++;
}
int create_root() {
  int vid = new_node(-1, 0, 0, 0);
  t[vid].suflink = 0;
  return vid;
}
int root = create_root();
inline int length(int vid) { return t[vid].r - t[vid].l; }
struct state {
  int vid;
  int pos;
  state(int vid, int pos) : vid(vid), pos(pos) {}
};
inline state go(state st, char c) {
  if (st.pos == length(st.vid)) {
    int to = t[st.vid].next[c - SC];
    return to == -1 ? state(-1, -1) : state(to, 1);
  }
  return s[t[st.vid].id][t[st.vid].l + st.pos] == c ? state(st.vid, st.pos + 1)
                                                    : state(-1, -1);
}
inline int split(state st) {
  if (st.pos == length(st.vid)) return st.vid;
  if (st.pos == 0) return t[st.vid].par;
  node &v = t[st.vid];
  int nvid = new_node(v.par, v.l, v.l + st.pos, v.id);
  t[v.par].next[s[v.id][v.l] - SC] = nvid;
  v.l += st.pos;
  v.par = nvid;
  t[nvid].next[s[v.id][v.l] - SC] = st.vid;
  return nvid;
}
state fast_go(int vid, int l, int r, int id) {
  if (l == r) return state(vid, length(vid));
  while (true) {
    int to_vid = t[vid].next[s[id][l] - SC];
    assert(to_vid >= 0);
    if (r - l <= length(to_vid)) return state(to_vid, r - l);
    l += length(to_vid);
    vid = to_vid;
  }
  throw 0;
}
int suflink(int vid) {
  node &v = t[vid];
  if (v.suflink == -1)
    v.suflink =
        split(fast_go(suflink(v.par), v.l + int(v.par == root), v.r, v.id));
  return v.suflink;
}
inline state ukk_add(state st, int i, int id) {
  while (true) {
    state to = go(st, s[id][i]);
    if (to.vid != -1) return to;
    int mid = split(st);
    t[mid].next[s[id][i] - SC] = new_node(mid, i, slen[id], id);
    st = state(suflink(mid), length(suflink(mid)));
    if (mid == root) return st;
  }
  throw 0;
}
void make_tree(int id) {
  state st(root, 0);
  for (int i = 0; i < slen[id]; ++i) st = ukk_add(st, i, id);
}
int dfs(int vid, int depth) {
  node &v = t[vid];
  bool leaf = true;
  int res = INT_MAX;
  for (int i = 0; i < ALPH; ++i)
    if (v.next[i] != -1) {
      leaf = false;
      int tmp = dfs(v.next[i], depth + length(v.next[i]));
      res = min(res, tmp);
      for (int j = 0; j < 2; ++j) v.res_cnt[j] += t[v.next[i]].res_cnt[j];
    }
  if (leaf) ++v.res_cnt[v.id];
  if (v.res_cnt[0] == 1 && v.res_cnt[1] == 1)
    res = min(res, depth - length(vid) + 1 - leaf);
  return res;
}
char s2[MAXN];
int main() {
  for (int i = 0; i < 2; ++i) {
    gets(&s[i][0]);
    slen[i] = strlen(&s[i][0]);
    s[i][slen[i]++] = 'z' + i + 1;
    make_tree(i);
  }
  int res = dfs(root, 0);
  if (res == INT_MAX) res = -1;
  printf("%d\n", res);
  return 0;
}
