#include <bits/stdc++.h>
using namespace std;
void rn(int n, int add[]) {
  int i;
  for (i = 1; i <= n; i++) {
    scanf("%d", add + i);
  }
}
char s[5005], t[5005];
int LCP1[5005][5005];
int LCP2[5005][5005];
int LCP[5005][5005];
int mU1[5005];
int mU2[5005];
void getLCP(char s[], char t[], int lcp[][5005]) {
  memset(lcp, 0, sizeof(lcp));
  int lens = strlen(s);
  int lent = strlen(t);
  int i;
  for (i = lens - 1; i >= 0; i--) {
    int j;
    for (j = lent - 1; j >= 0; j--) {
      if (s[i] == t[j]) {
        lcp[i][j] = lcp[i + 1][j + 1] + 1;
      } else {
        lcp[i][j] = 0;
      }
    }
  }
}
int main() {
  scanf("%s%s", &s, &t);
  getLCP(s, t, LCP);
  getLCP(s, s, LCP1);
  getLCP(t, t, LCP2);
  int i;
  int lens = strlen(s);
  int lent = strlen(t);
  for (i = 0; i < lens; i++) {
    int j;
    int mU = 0;
    for (j = 0; j < lens; j++) {
      if (i == j) continue;
      mU = max(mU, LCP1[i][j]);
    }
    mU1[i] = mU;
  }
  for (i = 0; i < lent; i++) {
    int j;
    int mU = 0;
    for (j = 0; j < lent; j++) {
      if (i == j) continue;
      mU = max(mU, LCP2[i][j]);
    }
    mU2[i] = mU;
  }
  int ans = 0x3f3f3f3f;
  for (i = 0; i < lens; i++) {
    int j;
    for (j = 0; j < lent; j++) {
      if (LCP[i][j] > max(mU1[i], mU2[j])) {
        ans = min(min(max(mU1[i], mU2[j]) + 1, LCP[i][j]), ans);
      }
    }
  }
  if (ans == 0x3f3f3f3f) {
    printf("-1");
    return 0;
  }
  printf("%d", ans);
}
