#include <bits/stdc++.h>
using namespace std;
int son[20010][28], fa[20010], len[20010], dp[20010][2];
int vis[20010];
int t, tail;
void init() {
  t = tail = 0;
  memset(son[t], 0, sizeof(son[t]));
  fa[t] = -1, len[t] = 0;
  dp[0][0] = dp[0][1] = 0;
}
void add(int x) {
  int p = tail, np = ++t;
  tail = np;
  memset(son[t], 0, sizeof(son[t]));
  len[np] = len[p] + 1;
  while (p + 1 && !son[p][x]) son[p][x] = np, p = fa[p];
  if (p == -1)
    fa[np] = 0;
  else {
    int q = son[p][x];
    if (len[q] == len[p] + 1)
      fa[np] = q;
    else {
      int nq = ++t;
      len[nq] = len[p] + 1;
      memcpy(son[nq], son[q], sizeof(son[q]));
      fa[nq] = fa[q];
      fa[q] = fa[np] = nq;
      while (p + 1 && son[p][x] == q) son[p][x] = nq, p = fa[p];
    }
  }
}
int ans;
void dfs(int u) {
  if (vis[u]) return;
  vis[u] = 1;
  dp[u][0] = dp[u][1] = 0;
  if (son[u][26]) dp[u][0] = 1;
  if (son[u][27]) dp[u][1] = 1;
  int v;
  for (int i = 0; i < 26; ++i)
    if (v = son[u][i]) {
      dfs(v);
      dp[u][0] += dp[v][0];
      dp[u][1] += dp[v][1];
    }
  if (u && dp[u][0] == 1 && dp[u][1] == 1) ans = min(ans, len[fa[u]] + 1);
}
char str[20010];
int main() {
  init();
  scanf("%s", str);
  for (int i = 0; str[i]; ++i) add(str[i] - 'a');
  add(26);
  scanf("%s", str);
  for (int i = 0; str[i]; ++i) add(str[i] - 'a');
  add(27);
  ans = 20010;
  dfs(0);
  if (ans == 20010) ans = -1;
  printf("%d\n", ans);
}
