#include <bits/stdc++.h>
using namespace std;
int n, m, cnt[15];
pair<int, int> a[15], b[15];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d %d", &a[i].first, &a[i].second);
  for (int i = 0; i < m; i++) scanf("%d %d", &b[i].first, &b[i].second);
  set<int> possible;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      memset(cnt, 0, sizeof cnt);
      cnt[a[i].first]++;
      cnt[a[i].second]++;
      cnt[b[j].first]++;
      cnt[b[j].second]++;
      int quant = 0, lst = -1;
      for (int k = 0; k < 10; k++)
        if (cnt[k] == 2) {
          quant++;
          lst = k;
        }
      if (quant == 1) possible.insert(lst);
    }
  }
  if (possible.size() == 1)
    printf("%d\n", *possible.begin());
  else {
    bool val = true;
    for (int i = 0; i < n; i++) {
      set<int> aux;
      for (int j = 0; j < m; j++) {
        memset(cnt, 0, sizeof cnt);
        cnt[a[i].first]++;
        cnt[a[i].second]++;
        cnt[b[j].first]++;
        cnt[b[j].second]++;
        int quant = 0, lst = -1;
        for (int k = 0; k < 10; k++)
          if (cnt[k] == 2) {
            quant++;
            lst = k;
          }
        if (quant == 1) aux.insert(lst);
      }
      if (aux.size() > 1) val = false;
    }
    for (int i = 0; i < m; i++) {
      set<int> aux;
      for (int j = 0; j < n; j++) {
        memset(cnt, 0, sizeof cnt);
        cnt[b[i].first]++;
        cnt[b[i].second]++;
        cnt[a[j].first]++;
        cnt[a[j].second]++;
        int quant = 0, lst = -1;
        for (int k = 0; k < 10; k++)
          if (cnt[k] == 2) {
            quant++;
            lst = k;
          }
        if (quant == 1) aux.insert(lst);
      }
      if (aux.size() > 1) val = false;
    }
    if (val)
      printf("0\n");
    else
      printf("-1\n");
  }
  return 0;
}
