#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void RST(T &A) {
  memset(A, 0, sizeof(A));
}
template <class T>
inline void FLC(T &A, int x) {
  memset(A, x, sizeof(A));
}
template <class T>
inline void CLR(T &A) {
  A.clear();
}
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long INFF = 1LL << 60;
const double EPS = 1e-9;
const double OO = 1e15;
const double PI = acos(-1.0);
template <class T>
inline void checkMin(T &a, const T b) {
  if (b < a) a = b;
}
template <class T>
inline void checkMax(T &a, const T b) {
  if (a < b) a = b;
}
template <class T>
inline T low_bit(T x) {
  return x & -x;
}
struct hash_map {
  const static int mod = 10007;
  int head[mod];
  struct hash_tables {
    unsigned long long key;
    int val;
    int next;
  } ele[5000 + 5];
  int N;
  int getHash(unsigned long long x) { return x % mod; }
  void init() {
    memset(head, 255, sizeof(head));
    N = 0;
  }
  void clear() {
    for (int i = 0; i < N; i++) head[getHash(ele[i].key)] = -1;
    N = 0;
  }
  int fint(unsigned long long x) {
    for (int i = head[getHash(x)]; i != -1; i = ele[i].next)
      if (ele[i].key == x) return i;
    return -1;
  }
  void insert(unsigned long long x) {
    int tmp = getHash(x);
    ele[N].key = x;
    ele[N].val = 0;
    ele[N].next = head[tmp];
    head[tmp] = N++;
  }
  int &operator[](unsigned long long x) {
    int tmp = fint(x);
    if (tmp == -1) {
      insert(x);
      return ele[N - 1].val;
    } else
      return ele[tmp].val;
  }
} miaoa, miaob;
const int N = 5009;
char a[N], b[N];
int n, m;
unsigned long long prea[N], preb[N];
unsigned long long exx[N];
int main() {
  miaoa.init();
  miaob.init();
  scanf("%s%s", a, b);
  n = strlen(a);
  exx[0] = 1;
  for (int i = 1; i < N; ++i) exx[i] = exx[i - 1] * MOD;
  unsigned long long ex = 1;
  for (int i = 0; i < n; ++i) {
    if (i == 0)
      prea[i] = a[i];
    else
      prea[i] = prea[i - 1] + ex * a[i];
    ex *= MOD;
  }
  m = strlen(b);
  ex = 1;
  for (int i = 0; i < m; ++i) {
    if (i == 0)
      preb[i] = b[i];
    else
      preb[i] = preb[i - 1] + ex * b[i];
    ex *= MOD;
  }
  int ans = -1;
  miaoa.init(), miaob.init();
  for (int l = 1; l <= n && l <= m && ans == -1; ++l) {
    miaoa.clear(), miaob.clear();
    for (int i = 0; i + l - 1 < n; ++i) {
      unsigned long long x = prea[i + l - 1];
      if (i) x -= prea[i - 1];
      x *= exx[N - i - 1];
      miaoa[x]++;
    }
    for (int i = 0; i + l - 1 < m; ++i) {
      unsigned long long x = preb[i + l - 1];
      if (i) x -= preb[i - 1];
      x *= exx[N - i - 1];
      miaob[x]++;
    }
    for (int i = 0; i + l - 1 < m; ++i) {
      unsigned long long x = preb[i + l - 1];
      if (i) x -= preb[i - 1];
      x *= exx[N - i - 1];
      if (miaoa.fint(x) != -1 && miaoa[x] == 1 && miaob[x] == 1) {
        ans = l;
        break;
      }
    }
  }
  cout << ans << endl;
}
