#include <bits/stdc++.h>
int n, m, counter[2][65536][15];
char str[5010], str2[5010], len[2][65536];
unsigned long long int hash[2][65536][15], P[5010];
const unsigned long long int base = 1000000007ULL;
void insert(int idx, unsigned long long int x) {
  int i;
  unsigned short y = x;
  for (i = 0; i < len[idx][y]; i++) {
    if (hash[idx][y][i] == x) {
      counter[idx][y][i]++;
      return;
    }
  }
  hash[idx][y][len[idx][y]] = x;
  counter[idx][y][len[idx][y]] = 1;
  len[idx][y]++;
}
bool search(int idx, unsigned long long int x) {
  int i;
  unsigned short y = x;
  for (i = 0; i < len[idx][y]; i++) {
    if (hash[idx][y][i] == x) {
      return (counter[idx][y][i] == 1);
    }
  }
  return false;
}
bool F(int l) {
  memset(len, 0, sizeof(len));
  int i, j, k, res = 0;
  unsigned long long int x = 0;
  x = 0;
  for (i = 0; i < l; i++) x = (x * base) + str[i];
  for (i = l; i <= n; i++) {
    insert(0, x);
    x = x - (P[l - 1] * str[i - l]);
    x = (x * base) + str[i];
  }
  x = 0;
  for (i = 0; i < l; i++) x = (x * base) + str2[i];
  for (i = l; i <= m; i++) {
    if (search(0, x)) {
      bool found = false;
      unsigned short y = x;
      for (j = 0; j < len[1][y]; j++) {
        if (hash[1][y][j] == x) {
          found = true;
          if (counter[1][y][j] == 1) res--;
          counter[1][y][j]++;
          break;
        }
      }
      if (!found) {
        res++;
        hash[1][y][len[1][y]] = x;
        counter[1][y][len[1][y]] = 1;
        len[1][y]++;
      }
    }
    x = x - (P[l - 1] * str2[i - l]);
    x = (x * base) + str2[i];
  }
  return (res > 0);
}
int main() {
  int i, j, k, l;
  P[0] = 1ULL;
  for (i = 1; i < 5010; i++) P[i] = (P[i - 1] * base);
  while (scanf("%s %s", str, str2) != EOF) {
    n = strlen(str), m = strlen(str2);
    for (l = 1; l <= ((n) < (m) ? (n) : (m)); l++) {
      if (F(l)) {
        printf("%d\n", l);
        break;
      }
    }
    if (l > ((n) < (m) ? (n) : (m))) puts("-1");
  }
  return 0;
}
