#include <bits/stdc++.h>
using namespace std;
const int MAXINT = 0x6fffffff;
const long long MAXLONG = (long long)1 << 60;
const int MAXN = 5005;
char s1[MAXN], s2[MAXN];
int len1, len2;
int d1[MAXN], d2[MAXN];
int dp[MAXN][MAXN];
void calc(char s[], int d[]) {
  int len = strlen(s);
  memset(dp, 0, sizeof(dp));
  for (long long i = (1); i < (len + 1); i += 1) {
    for (long long j = (1); j < (len + 1); j += 1) {
      if (s[i - 1] == s[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = 0;
      }
    }
  }
  for (long long i = (1); i < (len + 1); i += 1) {
    d[i] = 0;
    for (long long j = (1); j < (len + 1); j += 1) {
      if (i == j) continue;
      d[i] = max(d[i], dp[i][j]);
    }
  }
}
int main() {
  while (scanf("%s %s", s1, s2) != -1) {
    len1 = strlen(s1);
    len2 = strlen(s2);
    calc(s1, d1);
    calc(s2, d2);
    memset(dp, 0, sizeof(dp));
    for (long long i = (1); i < (len1 + 1); i += 1) {
      for (long long j = (1); j < (len2 + 1); j += 1) {
        if (s1[i - 1] == s2[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = 0;
        }
      }
    }
    int ans = 5005;
    for (long long i = (1); i < (len1 + 1); i += 1) {
      for (long long j = (1); j < (len2 + 1); j += 1) {
        if (dp[i][j] > max(d1[i], d2[j])) {
          ans = min(ans, 1 + max(d1[i], d2[j]));
        }
      }
    }
    if (ans == 5005) {
      puts("-1");
    } else {
      printf("%d\n", ans);
    }
  }
  return 0;
}
