#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f;
using namespace std;
const int MAX_N = 5010;
struct state {
  int len, link, cnt;
  int next[26];
};
struct suf_aut {
  state st[2 * MAX_N];
  int sz;
  int last;
  int grau[2 * MAX_N];
  void init() {
    sz = 1;
    last = 0;
    st[0].len = 0;
    st[0].link = -1;
    st[0].cnt = 0;
    memset((st[0].next), (-1), sizeof(st[0].next));
  }
  void extend(int c) {
    int cur = sz++;
    st[cur].len = st[last].len + 1;
    st[cur].cnt = 1;
    memset((st[cur].next), (-1), sizeof(st[cur].next));
    int p;
    for (p = last; p != -1 && st[p].next[c] == -1; p = st[p].link) {
      st[p].next[c] = cur;
    }
    if (p == -1) {
      st[cur].link = 0;
    } else {
      int q = st[p].next[c];
      if (st[p].len + 1 == st[q].len) {
        st[cur].link = q;
      } else {
        int clone = sz++;
        st[clone].len = st[p].len + 1;
        memcpy(st[clone].next, st[q].next, sizeof st[q].next);
        st[clone].link = st[q].link;
        for (; p != -1 && st[p].next[c] == q; p = st[p].link) {
          st[p].next[c] = clone;
        }
        st[q].link = st[cur].link = clone;
      }
    }
    last = cur;
  }
  void calcula_contagem() {
    memset((grau), (0), sizeof(grau));
    queue<int> fila;
    while (!fila.empty()) fila.pop();
    for (int i = 1; i < sz; ++i) grau[st[i].link]++;
    for (int i = 1; i < sz; ++i) {
      if (grau[i] == 0) fila.push(i);
    }
    while (!fila.empty()) {
      int k = fila.front();
      fila.pop();
      if (k > 0) {
        st[st[k].link].cnt += st[k].cnt;
        if ((--grau[st[k].link]) == 0) fila.push(st[k].link);
      }
    }
  }
};
char str1[MAX_N];
char str2[MAX_N];
suf_aut a, b;
int rsp;
void go(int st1, int st2, int len) {
  if (len && a.st[st1].cnt == 1 && b.st[st2].cnt == 1) {
    rsp = min(rsp, len);
    return;
  }
  for (int i = (0); i < (26); ++i) {
    if (a.st[st1].next[i] != -1 && b.st[st2].next[i] != -1)
      go(a.st[st1].next[i], b.st[st2].next[i], len + 1);
  }
}
int main() {
  scanf("%s %s", str1, str2);
  int len1 = strlen(str1), len2 = strlen(str2);
  a.init();
  b.init();
  for (int i = (0); i < (len1); ++i) a.extend(str1[i] - 'a');
  for (int i = (0); i < (len2); ++i) b.extend(str2[i] - 'a');
  a.calcula_contagem();
  b.calcula_contagem();
  rsp = inf;
  go(0, 0, 0);
  if (rsp == inf)
    printf("-1\n");
  else
    printf("%d\n", rsp);
  return 0;
}
