#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int MOD = 1000000007;
char s1[5003], s2[5003];
int dp[5001][5001], cnt1[5003], cnt2[5003];
int p[5003];
set<string> s;
void KMP(string &t, char s[], int cnt[]) {
  int n = strlen(s);
  int m = t.size();
  p[0] = -1;
  for (int i = 1, j = p[0]; i < m; i++) {
    while (~j && t[i] != t[j + 1]) j = p[j];
    if (t[i] == t[j + 1]) j++;
    p[i] = j;
  }
  for (int i = 0, j = p[0]; i < n; i++) {
    while (~j && s[i] != t[j + 1]) j = p[j];
    if (s[i] == t[j + 1]) j++;
    if (~j) cnt[j]++;
  }
  for (int i = m - 1; i >= 0; i--) {
    cnt[p[i]] += cnt[i];
  }
}
int check(string &str) {
  int ans = INF;
  memset(cnt1, 0, sizeof(cnt1));
  KMP(str, s1, cnt1);
  memset(cnt2, 0, sizeof(cnt2));
  KMP(str, s2, cnt2);
  for (int i = 0; i < str.size(); i++) {
    if (cnt1[i] == 1 && cnt2[i] == 1) {
      ans = i + 1;
      break;
    }
  }
  return ans;
}
int main() {
  int n, m;
  int ans;
  scanf("%s%s", s1, s2);
  n = strlen(s1);
  m = strlen(s2);
  ans = n + 1;
  for (int i = 0; i < n; i++) {
    string t;
    t.assign(s1, i, n - i);
    ans = min(ans, check(t));
  }
  for (int i = 0; i < m; i++) {
    string t;
    t.assign(s2, i, m - i);
    ans = min(ans, check(t));
  }
  if (ans == n + 1) ans = -1;
  printf("%d\n", ans);
  return 0;
}
