#include <bits/stdc++.h>
namespace MyMinMax {
template <typename T>
inline T mn(T x, T y) {
  return x < y ? x : y;
}
template <typename T>
inline T mx(T x, T y) {
  return x > y ? x : y;
}
template <typename T>
inline void chmin(T &x, T y) {
  (x > y) && (x = y);
}
template <typename T>
inline void chmax(T &x, T y) {
  (x < y) && (x = y);
}
}  // namespace MyMinMax
using namespace MyMinMax;
namespace IO {
char BB[1 << 18], *SS = BB, *TT = BB;
template <typename T = int>
inline T read() {
  T x = 0;
  int fu = 1;
  char c =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (c > 57 || c < 48) {
    if (c == 45) fu = -1;
    c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
             ? EOF
             : *SS++);
  }
  while (c <= 57 && c >= 48) {
    x = x * 10 + c - 48;
    c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
             ? EOF
             : *SS++);
  }
  x *= fu;
  return x;
}
template <typename T>
inline void read(T &x) {
  x = 0;
  int fu = 1;
  char c =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (c > 57 || c < 48) {
    if (c == 45) fu = -1;
    c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
             ? EOF
             : *SS++);
  }
  while (c <= 57 && c >= 48) {
    x = x * 10 + c - 48;
    c = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
             ? EOF
             : *SS++);
  }
  x *= fu;
}
template <typename T>
inline void read(T *bg, T *ed) {
  while (bg != ed) read(*bg++);
}
inline void read(char &ch) {
  ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
            ? EOF
            : *SS++);
  while (ch <= 32)
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
              ? EOF
              : *SS++);
}
inline void read(char *s) {
  char ch =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (ch <= 32)
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
              ? EOF
              : *SS++);
  while (ch > 32)
    *s++ = ch,
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
              ? EOF
              : *SS++);
  *s = '\0';
}
inline void sread(char *s) {
  char ch =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (ch < 32)
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
              ? EOF
              : *SS++);
  while (ch >= 32)
    *s++ = ch,
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
              ? EOF
              : *SS++);
  *s = '\0';
}
inline void pread(char *&s) {
  char ch =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (ch <= 32)
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
              ? EOF
              : *SS++);
  while (ch > 32)
    *s++ = ch,
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
              ? EOF
              : *SS++);
  *s = '\0';
}
inline void spread(char *&s) {
  char ch =
      (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
           ? EOF
           : *SS++);
  while (ch < 32)
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
              ? EOF
              : *SS++);
  while (ch >= 32)
    *s++ = ch,
    ch = (SS == TT && (TT = (SS = BB) + fread(BB, 1, 1 << 18, stdin), SS == TT)
              ? EOF
              : *SS++);
  *s = '\0';
}
template <typename T, typename... Args>
inline void read(T &x, Args &...args) {
  read(x);
  read(args...);
}
char out[1 << 18], *Out = out;
inline void putcha(char x) {
  *Out++ = x;
  if (Out - out >= (1 << 18)) fwrite(out, 1, Out - out, stdout), Out = out;
}
template <typename T>
inline void fprint(T x) {
  if (x < 0) putcha(45), x = -x;
  if (x > 9) fprint(x / 10);
  putcha(x % 10 + 48);
}
template <typename T>
inline void print(T x) {
  fprint(x);
  putcha(10);
}
inline void print(char *ch) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
}
inline void put(char *ch) {
  while (*ch != '\0') putcha(*(ch++));
}
inline void print(const char *ch) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
}
inline void put(const char *ch) {
  while (*ch != '\0') putcha(*(ch++));
}
template <typename T, typename... Args>
inline void print(T x, Args... args) {
  fprint(x);
  putcha(32);
  print(args...);
}
template <typename... Args>
inline void print(const char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(32);
  print(args...);
}
template <typename... Args>
inline void print(char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(32);
  print(args...);
}
template <typename T, typename... Args>
inline void printl(T x, Args... args) {
  fprint(x);
  putcha(10);
  print(args...);
}
template <typename... Args>
inline void printl(const char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
  print(args...);
}
template <typename... Args>
inline void printl(char *ch, Args... args) {
  while (*ch != '\0') putcha(*(ch++));
  putcha(10);
  print(args...);
}
template <typename T>
inline void sprint(T x) {
  fprint(x);
  putcha(32);
}
template <typename T, typename... Args>
inline void sprint(T x, Args... args) {
  fprint(x);
  putcha(32);
  sprint(args...);
}
template <typename T>
inline void sprint(T *bg, T *ed) {
  while (bg != ed) sprint(*bg++);
}
template <typename T>
inline void print(T *bg, T *ed) {
  while (bg != ed) sprint(*bg++);
  putcha(10);
}
template <typename T>
inline void printl(T *bg, T *ed) {
  while (bg != ed) print(*bg++);
}
class AutoFlush {
 public:
  ~AutoFlush() { fwrite(out, 1, Out - out, stdout); }
} __AutoFlush;
}  // namespace IO
using namespace IO;
struct Trie {
  int t[10005][26], tot;
  inline void ins(char *p) {
    int rt = 0;
    for (char *it = p; *it != '\0'; ++it) {
      if (!t[rt][*it - 97]) t[rt][*it - 97] = ++tot;
      rt = t[rt][*it - 97];
    }
  }
};
int N;
char s1[10005], s2[10005];
struct SAM : virtual public Trie {
  int len[10005 << 1], link[10005 << 1];
  int nxt[10005 << 1][26];
  int sz, lst[10005 << 1];
  SAM() {
    len[0] = 0;
    link[0] = -1;
    ++sz;
  }
  inline int extend(int c, int pre) {
    int x = sz++;
    len[x] = len[pre] + 1;
    int it = pre;
    while (~it && !nxt[it][c]) {
      nxt[it][c] = x;
      it = link[it];
    }
    if (!~it)
      link[x] = 0;
    else {
      int t = nxt[it][c];
      if (len[it] + 1 == len[t])
        link[x] = t;
      else {
        int y = sz++;
        len[y] = len[it] + 1;
        memcpy(nxt[y], nxt[t], sizeof(nxt[y]));
        link[y] = link[t];
        while (~it && nxt[it][c] == t) {
          nxt[it][c] = y;
          it = link[it];
        }
        link[t] = link[x] = y;
      }
    }
    return x;
  }
  int buf[10005];
  inline void layer() {
    int *hd = buf, *ed = buf + 1;
    while (hd != ed) {
      int x = *hd++;
      for (int i = (0); i <= (25); ++i)
        if (t[x][i]) {
          *ed++ = t[x][i];
          lst[t[x][i]] = extend(i, lst[x]);
        }
    }
  }
  int c[10005 << 1][2];
  inline void init() {
    layer();
    int rt = 0;
    for (char *it = s1; *it != '\0'; ++it) {
      rt = nxt[rt][*it - 97];
      ++c[rt][0];
    }
    rt = 0;
    for (char *it = s2; *it != '\0'; ++it) {
      rt = nxt[rt][*it - 97];
      ++c[rt][1];
    }
  }
  int cnt[10005], a[10005 << 1], ans = 0x3f3f3f3f;
  inline void solve() {
    init();
    for (int i = (0); i <= (sz - 1); ++i) ++cnt[len[i]];
    for (int i = (1); i <= (N); ++i) cnt[i] += cnt[i - 1];
    for (int i = (0); i <= (sz - 1); ++i) a[--cnt[len[i]]] = i;
    for (int i = (sz - 1); i >= (1); --i) {
      int x = a[i];
      c[link[x]][0] += c[x][0];
      c[link[x]][1] += c[x][1];
      if (c[x][0] == 1 && c[x][1] == 1) chmin(ans, len[link[x]] + 1);
    }
    print(ans == 0x3f3f3f3f ? -1 : ans);
  }
} t;
int main() {
  read(s1);
  read(s2);
  N = mx(strlen(s1), strlen(s2));
  t.ins(s1);
  t.ins(s2);
  t.solve();
}
