#include <bits/stdc++.h>
using namespace std;
vector<set<int> > AdjList1, AdjList2;
int main() {
  int n, m, i, j;
  vector<pair<int, int> > a, b;
  scanf("%d %d", &n, &m);
  AdjList1.assign(n + 1, set<int>());
  AdjList2.assign(m + 1, set<int>());
  for (i = 0; i < n; i++) {
    int bil1, bil2;
    scanf("%d %d", &bil1, &bil2);
    a.push_back(pair<int, int>(bil1, bil2));
  }
  for (i = 0; i < m; i++) {
    int bil1, bil2;
    scanf("%d %d", &bil1, &bil2);
    b.push_back(pair<int, int>(bil1, bil2));
  }
  for (i = 0; i < n; i++) {
    int a1 = a[i].first, a2 = a[i].second;
    for (j = 0; j < m; j++) {
      int b1 = b[j].first, b2 = b[j].second;
      if ((a1 == b1 && a2 == b2) || (a1 == b2 && a2 == b1)) {
        continue;
      }
      if (a1 == b1 || a1 == b2 || a2 == b1 || a2 == b2) {
        if (a1 == b1 || a1 == b2) {
          AdjList1[i].insert(a1);
          AdjList2[j].insert(a1);
        } else if (a2 == b1 || a2 == b2) {
          AdjList1[i].insert(a2);
          AdjList2[j].insert(a2);
        }
      }
    }
  }
  int jumlah1 = 0, cnt1 = 0;
  bool isValid = true;
  int ans;
  int minUkuran = 1000000000;
  set<int> s;
  for (i = 0; i < n; i++) {
    int ukuran = AdjList1[i].size();
    if (ukuran > 1) {
      minUkuran = min(minUkuran, ukuran);
      isValid = false;
    } else if (ukuran == 1) {
      ans = *AdjList1[i].begin();
      s.insert(ans);
      jumlah1++;
    }
  }
  for (i = 0; i < m; i++) {
    int ukuran = AdjList2[i].size();
    if (ukuran > 1) {
      minUkuran = min(minUkuran, ukuran);
      isValid = false;
    } else if (ukuran == 1) {
      ans = *AdjList2[i].begin();
      s.insert(ans);
      cnt1++;
    }
  }
  if (!isValid) {
    printf("-1\n");
  } else {
    if (s.size() == 1) {
      printf("%d\n", *(s.begin()));
    } else {
      printf("0\n");
    }
  }
  return 0;
};
