#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int a[N], b[N];
inline int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch == '-' ? o = -1 : 0, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return o * x;
}
int main() {
  int n, m, t = 0;
  bool flg = 1;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) a[i] = (1 << gi()) | (1 << gi());
  for (int i = 1; i <= m; i++) b[i] = (1 << gi()) | (1 << gi());
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int x = a[i] & b[j];
      if (a[i] != b[j] && x) {
        if (!t)
          t = x;
        else if (t != x) {
          flg = 0;
          break;
        }
      }
    }
    if (!flg) break;
  }
  if (flg) {
    for (int i = 1; i < 10; i++)
      if (t >> i & 1) cout << i;
    return 0;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int x = a[i] & b[j];
      if (a[i] != b[j] && x) {
        for (int k = 1; k <= m; k++)
          if (k != j && a[i] != b[k] && a[i] & b[k] && (a[i] & b[k]) != x)
            return cout << -1, 0;
        for (int k = 1; k <= n; k++)
          if (i != k && a[k] != b[j] && a[k] & b[j] && (a[k] & b[j]) != x)
            return cout << -1, 0;
      }
    }
  cout << 0;
  return 0;
}
