#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
void maximize(T &lhs, U rhs) {
  if (lhs < rhs) lhs = rhs;
}
template <class T, class U>
void minimize(T &lhs, U rhs) {
  if (lhs > rhs) lhs = rhs;
}
template <class T, class U>
void maximize(T &lhs, const initializer_list<U> &rhs) {
  U max_rhs = max(rhs);
  if (lhs < max_rhs) {
    lhs = max_rhs;
  }
}
template <class T, class U>
void minimize(T &lhs, const initializer_list<U> &rhs) {
  U min_rhs = min(rhs);
  if (lhs > min_rhs) {
    lhs = min_rhs;
  }
}
template <class T>
T tabs(T x) {
  return (x < (T)0 ? -x : x);
}
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
mt19937 generator(chrono::system_clock::now().time_since_epoch().count());
template <class T = int>
T __rand(T range = numeric_limits<T>::max()) {
  return (T)(generator() % range);
}
namespace Task {
class SuffixArray {
 public:
  static const int lg = 13;
  vector<vector<int> > suff;
  vector<int> lcp;
  vector<int> ranks;
  vector<pair<pair<int, int>, int> > curRanks;
  int lstr = -1;

 public:
  SuffixArray(const string &str) : lstr((int)((str).size())) {
    suff.resize(lg + 2, vector<int>(lstr + 2, 0));
    curRanks.resize(lstr, {{0, 0}, 0});
    ranks.resize(lstr, 0);
    lcp.resize(lstr, 0);
    for (int i = 0; i < lstr; ++i) {
      suff[0][i] = str[i] - 'a';
    }
    for (int j = 1; j <= lg; ++j) {
      for (int i = 0; i < lstr; ++i) {
        curRanks[i] = {
            {suff[j - 1][i], (i + ((1LL) << (j - 1)) < lstr
                                  ? suff[j - 1][i + ((1LL) << (j - 1))]
                                  : -1)},
            i};
      }
      sort(curRanks.begin(), curRanks.end());
      for (int i = 0; i < lstr; ++i) {
        suff[j][curRanks[i].second] =
            (i and curRanks[i].first == curRanks[i - 1].first
                 ? suff[j][curRanks[i - 1].second]
                 : i);
      }
    }
    for (int i = 0; i < lstr; ++i) {
      ranks[i] = curRanks[i].second;
    }
  }
  int computeLcp(int rx, int ry) {
    if (rx > ry) {
      swap(rx, ry);
    }
    int ret = 0;
    for (int j = lg; ~j; --j) {
      if (ry + ((1LL) << (j)) - 1 < lstr) {
        if (suff[j][rx] == suff[j][ry]) {
          ret |= ((1LL) << (j));
          rx += ((1LL) << (j));
          ry += ((1LL) << (j));
        }
      }
    }
    return ret;
  }
  void buildLcp() {
    for (int i = 0; i < lstr - 1; ++i) {
      lcp[i] = computeLcp(ranks[i], ranks[i + 1]);
    }
  }
};
SuffixArray *sa;
void solve() {
  string s, t;
  cin >> s >> t;
  sa = new SuffixArray(s + "#" + t);
  sa->buildLcp();
  int ans = numeric_limits<int>::max();
  auto ok = [&](int i, int j) {
    if (i > j) {
      swap(i, j);
    }
    return (0 <= i and i < (int)((s).size()) and (int)((s).size()) < j and
            j <= (int)((s).size()) + (int)((t).size()));
  };
  for (int i = 0; i < sa->lstr - 1; ++i) {
    if (i == 0) {
      if (sa->lcp[i] > sa->lcp[i + 1]) {
        if (ok(sa->ranks[i], sa->ranks[i + 1])) {
          minimize(ans, sa->lcp[i + 1] + 1);
        }
      }
    } else if (i == sa->lstr - 2) {
      if (sa->lcp[i - 1] < sa->lcp[i]) {
        if (ok(sa->ranks[i], sa->ranks[i + 1])) {
          minimize(ans, sa->lcp[i - 1] + 1);
        }
      }
    } else if (sa->lcp[i - 1] < sa->lcp[i] and sa->lcp[i] > sa->lcp[i + 1]) {
      if (ok(sa->ranks[i], sa->ranks[i + 1])) {
        minimize(ans, 1 + max(sa->lcp[i - 1], sa->lcp[i + 1]));
      }
    }
  }
  cout << (ans == numeric_limits<int>::max() ? -1 : ans) << '\n';
}
}  // namespace Task
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  Task::solve();
}
