#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[20], b[20];
inline int check(int x, int y) {
  int cnt = 0, k;
  if (a[x].first == b[y].first) ++cnt, k = a[x].first;
  if (a[x].first == b[y].second) ++cnt, k = a[x].first;
  if (a[x].second == b[y].first) ++cnt, k = a[x].second;
  if (a[x].second == b[y].second) ++cnt, k = a[x].second;
  if (cnt == 1) return k;
  return 0;
}
int c[20], d[20];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &a[i].first, &a[i].second);
  }
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &b[i].first, &b[i].second);
  }
  bool flag = true;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= 9; ++j) d[j] = 0;
    for (int j = 1; j <= m; ++j) {
      int k = check(i, j);
      if (k) d[k]++;
    }
    int cnt = 0, res;
    for (int j = 1; j <= 9; ++j)
      if (d[j]) {
        ++cnt;
        res = j;
      }
    if (cnt > 1) flag = false;
    if (cnt == 1) c[res]++;
  }
  if (!flag) return puts("-1");
  bool flag1 = false;
  int t = 0, wt;
  for (int i = 1; i <= 9; ++i) {
    if (c[i]) ++t, wt = i;
  }
  if (t > 1) flag1 = true;
  memset(c, 0, sizeof(c));
  for (int j = 1; j <= m; ++j) {
    for (int i = 1; i <= 9; ++i) d[i] = 0;
    for (int i = 1; i <= n; ++i) {
      int k = check(i, j);
      if (k) d[k]++;
    }
    int cnt = 0, res;
    for (int i = 1; i <= 9; ++i) {
      if (d[i]) {
        ++cnt;
        res = i;
      }
    }
    if (cnt > 1) flag = false;
    if (cnt == 1) c[res]++;
  }
  if (!flag) return puts("-1");
  t = 0;
  for (int i = 1; i <= 9; ++i) {
    if (c[i]) ++t, wt = i;
  }
  if (t > 1 || flag1) return puts("0");
  printf("%d\n", wt);
}
