#include <bits/stdc++.h>
using namespace std;
vector<int> getZ(string T, string P) {
  vector<int> Z;
  string w = T + "$" + P;
  int n = w.size();
  Z.resize(n);
  for (int i = 0; i < n; i++) Z[i] = 0;
  int l, r;
  l = r = 0;
  for (int i = 1; i < n; i++) {
    if (r < i) {
      l = i;
      int j;
      for (j = i; j < n; j++)
        if (w[j] != w[j - i]) break;
      r = j - 1;
      Z[i] = r - l + 1;
    } else {
      Z[i] = min(Z[i - l], r - i + 1);
      if (Z[i] + i > r) {
        int j;
        for (j = r + 1; j < n; j++)
          if (w[j] != w[j - i]) break;
        if (r != j - 1) {
          l = i;
          r = j - 1;
          Z[i] = r - l + 1;
        }
      }
    }
  }
  return Z;
}
int main() {
  string x, y;
  cin >> x >> y;
  int n, m;
  n = x.size();
  m = y.size();
  int ret = -1;
  vector<int> res;
  for (int i = 0; i < n; i++) {
    string str = x.substr(i, n);
    vector<int> z = getZ(str, x + "&" + y);
    int mn, mx;
    bool val = false;
    mn = mx = 0;
    int st = str.size() + 1;
    for (int j = 0; j < n; j++) {
      if (z[j + st] > mx) {
        mn = mx + 1;
        mx = z[j + st];
        val = true;
      } else if (z[j + st] == mx) {
        val = false;
      } else {
        mn = max(mn, z[j + st] + 1);
      }
    }
    if (!val) continue;
    st += n + 1;
    int mn2, mx2;
    bool val2 = false;
    mn2 = mx2 = 0;
    for (int j = 0; j < m; j++) {
      if (z[j + st] > mx2) {
        mn2 = mx2 + 1;
        mx2 = z[j + st];
        val2 = true;
      } else if (z[j + st] == mx2) {
        val2 = false;
      } else {
        mn2 = max(mn2, z[j + st] + 1);
      }
    }
    if (!val2) continue;
    int jum = 1;
    if (mn <= mx2 && mn >= mn2) {
      jum = max(jum, mn);
      if (ret == -1)
        ret = mn;
      else
        ret = min(ret, mn);
    }
    if (mn2 <= mx && mn2 >= mn) {
      jum = max(jum, mn2);
      if (ret == -1)
        ret = mn2;
      else
        ret = min(ret, mn2);
    }
  }
  cout << ret << endl;
  return 0;
}
