#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline int read();
const int M = 10016, MOD = 1000000007;
int sz, lst;
int ch[M << 1][128], len[M << 1], link[M << 1], cnt[M << 1];
int firstpos[M << 1], lastpos[M << 1];
void extend(const char *s) {
  for (int i = 0; s[i]; ++i) {
    int c = s[i];
    int cur = ++sz;
    len[cur] = len[lst] + 1;
    int p = lst;
    while (!ch[p][c]) {
      ch[p][c] = cur;
      p = link[p];
    }
    if (ch[p][c] != cur) {
      int q = ch[p][c];
      if (len[p] + 1 == len[q])
        link[cur] = q;
      else {
        int clone = ++sz;
        memcpy(ch[clone], ch[q], sizeof(ch[q]));
        link[clone] = link[q];
        firstpos[clone] = firstpos[q];
        lastpos[clone] = lastpos[q];
        len[clone] = len[p] + 1;
        while (ch[p][c] == q) {
          ch[p][c] = clone;
          p = link[p];
        }
        link[q] = link[cur] = clone;
      }
    }
    lst = cur;
    ++cnt[cur];
    firstpos[cur] = lastpos[cur] = i;
  }
  vector<int> nodes;
  for (int i = 1; i <= sz; ++i) nodes.push_back(i);
  sort(nodes.begin(), nodes.end(),
       [&](int a, int b) { return len[a] > len[b]; });
  for (auto u : nodes) {
    firstpos[link[u]] = min(firstpos[link[u]], firstpos[u]);
    lastpos[link[u]] = max(lastpos[link[u]], lastpos[u]);
    cnt[link[u]] += cnt[u];
  }
}
char tex[M];
int main(void) {
  scanf("%s", tex);
  int n = strlen(tex);
  tex[n] = '#';
  scanf("%s", tex + n + 1);
  extend(tex);
  int ans = -1;
  for (int i = 1; i <= sz; ++i) {
    if (cnt[i] == 2 && firstpos[i] < n && lastpos[i] > n) {
      if (ans == -1 || len[link[i]] + 1 < ans) ans = len[link[i]] + 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
