#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000000")
using namespace std;
const long long int INF = 2e9 + 1;
char s[50000], s2[50000];
struct automato {
  struct leaf {
    int link, len;
    map<char, int> nex;
    bool cl;
    leaf() : cl(false) {}
  };
  vector<leaf> tree;
  vector<int> cnt, used;
  int sz, pre;
  automato() {}
  void init() {
    sz = 1;
    pre = 0;
    tree.resize(2 * 50000);
    cnt.resize(2 * 50000, 0);
    used.resize(2 * 50000, 0);
    tree[0].link = -1;
    tree[0].len = 0;
  };
  void add_s(char c) {
    int cur = sz++;
    tree[cur].len = tree[pre].len + 1;
    int temp = pre;
    for (; temp != -1 && !tree[temp].nex.count(c); temp = tree[temp].link) {
      tree[temp].nex[c] = cur;
    }
    if (temp == -1) {
      tree[cur].link = 0;
    } else {
      int p = tree[temp].nex[c];
      if (tree[p].len == tree[temp].len + 1) {
        tree[cur].link = p;
      } else {
        int clone = sz++;
        tree[clone].nex = tree[p].nex;
        tree[clone].len = tree[temp].len + 1;
        tree[clone].link = tree[p].link;
        tree[clone].cl = true;
        tree[p].link = clone;
        tree[cur].link = clone;
        for (; temp != -1 && tree[temp].nex.count(c) && tree[temp].nex[c] == p;
             temp = tree[temp].link) {
          tree[temp].nex[c] = clone;
        }
      }
    }
    pre = cur;
  };
  void add_str(char *s) {
    int n = strlen(s);
    for (int i = 0; i < (n); i++) {
      add_s(s[i]);
    }
  }
  void dfs() {
    vector<pair<int, int> > len;
    for (int i = 0; i < (sz); i++) {
      len.push_back(make_pair(tree[i].len, i));
    }
    sort(len.begin(), len.end());
    reverse(len.begin(), len.end());
    for (int i = 0; i < (sz); i++) {
      int id = len[i].second;
      if (!tree[id].cl) cnt[id]++;
      if (tree[id].link != -1) cnt[tree[id].link] += cnt[id];
    }
  }
  int nx(int id, char c) {
    if (!tree[id].nex.count(c)) return -1;
    return tree[id].nex[c];
  }
  int get_cnt(int now) { return cnt[now]; }
};
int main() {
  scanf("%s%s", &s, &s2);
  int n = strlen(s), m = strlen(s2);
  automato a, b;
  a.init();
  b.init();
  a.add_str(s);
  b.add_str(s2);
  a.dfs();
  b.dfs();
  int out = INF;
  for (int i = 0; i < n; i++) {
    int st = 0, st2 = 0;
    for (int j = i; j < n; j++) {
      st = a.nx(st, s[j]);
      st2 = b.nx(st2, s[j]);
      if (st == -1 || st2 == -1) break;
      if (a.get_cnt(st) == 1 && b.get_cnt(st2) == 1) {
        out = min(out, j - i + 1);
        break;
      }
    }
  }
  cout << (out == INF ? -1 : out);
}
