#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010;
int dp[maxn][maxn], sa[maxn], sb[maxn];
char a[maxn], b[maxn];
void gao(char *p, char *q, int n, int m) {
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (p[i] == q[j]) dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]);
}
int main() {
  scanf("%s%s", a + 1, b + 1);
  int n = strlen(a + 1), m = strlen(b + 1);
  gao(a, a, n, n);
  for (int i = 1; i <= n; ++i) sa[i] = 1;
  for (int i = 1; i <= m; ++i) sb[i] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (i != j) sa[i] = max(sa[i], dp[i][j] + 1);
  gao(b, b, m, m);
  for (int i = 1; i <= m; ++i)
    for (int j = 1; j <= m; ++j)
      if (i != j) sb[i] = max(sb[i], dp[i][j] + 1);
  gao(a, b, n, m);
  int ans = -1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (dp[i][j] >= sa[i] && dp[i][j] >= sb[j])
        if (max(sa[i], sb[j]) != 0 && (ans < 0 || ans > max(sa[i], sb[j])))
          ans = max(sa[i], sb[j]);
  cout << ans << endl;
  return 0;
}
