#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 7;
const int INF = 1e9 + 7;
char stra[N];
int lena, lima[N];
char strb[N];
int lenb, limb[N];
int lcp[N][N];
void gao(char *str1, char *str2) {
  int len1 = strlen(str1);
  int len2 = strlen(str2);
  for (int i = (0); i < (len1 + 1); i++) lcp[i][len2] = 0;
  for (int j = (0); j < (len2 + 1); j++) lcp[len1][j] = 0;
  for (int i = (len1 - 1); i >= (0); i--)
    for (int j = (len2 - 1); j >= (0); j--)
      lcp[i][j] = (str1[i] == str2[j]) * (lcp[i + 1][j + 1] + 1);
}
int main() {
  scanf(" %s", stra);
  lena = strlen(stra);
  scanf(" %s", strb);
  lenb = strlen(strb);
  gao(stra, stra);
  for (int i = (0); i < (lena); i++)
    for (int j = (0); j < (lena); j++)
      if (j != i) lima[i] = max(lima[i], lcp[i][j]);
  gao(strb, strb);
  for (int i = (0); i < (lenb); i++)
    for (int j = (0); j < (lenb); j++)
      if (j != i) limb[i] = max(limb[i], lcp[i][j]);
  gao(stra, strb);
  int ans = INF;
  for (int i = (0); i < (lena); i++)
    for (int j = (0); j < (lenb); j++)
      if (lcp[i][j] > max(lima[i], limb[j]))
        ans = min(ans, max(lima[i], limb[j]) + 1);
  if (ans == INF) ans = -1;
  printf("%d", ans);
}
