#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const double pi = 2 * acos(0.0);
int iINF = 0x3f3f3f3f;
double dINF = 1.0 / 0.0;
int lcp[5005][5005];
string s1, s2;
void lcpf(string a, string b) {
  memset(lcp, 0, sizeof lcp);
  int lena = a.length(), lenb = b.length();
  int m;
  for (int i = lena - 1; i >= 0; i--) {
    for (int j = lenb - 1; j >= 0; j--) {
      if (a[i] == b[j]) lcp[i][j] = 1 + lcp[i + 1][j + 1];
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> s1 >> s2;
  int lcp1[5005], m, lcp2[5005], len1 = s1.length(), len2 = s2.length();
  memset(lcp1, 0, sizeof lcp2);
  memset(lcp2, 0, sizeof lcp2);
  lcpf(s1, s1);
  for (int i = 0; i < len1; i++) {
    m = 0;
    for (int j = 0; j < len1; j++) {
      if (i == j) continue;
      m = max(m, lcp[i][j]);
    }
    lcp1[i] = m;
  }
  lcpf(s2, s2);
  for (int i = 0; i < len2; i++) {
    m = 0;
    for (int j = 0; j < len2; j++) {
      if (i == j) continue;
      m = max(m, lcp[i][j]);
    }
    lcp2[i] = m;
  }
  lcpf(s1, s2);
  m = INT_MAX;
  for (int i = 0; i < len1; i++) {
    for (int j = 0; j < len2; j++) {
      if (lcp[i][j] >= max(lcp1[i], lcp2[j]) + 1)
        m = min(m, max(lcp1[i], lcp2[j]) + 1);
    }
  }
  if (m == INT_MAX) m = -1;
  cout << m;
}
