#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[20], b[20];
set<int> st1[20], st2[20], st;
set<int>::iterator it;
void Add1(int i, int j) {
  int cnt = 0, same;
  if (a[i].first == b[j].first) {
    cnt++;
    same = a[i].first;
  }
  if (a[i].first == b[j].second) {
    cnt++;
    same = a[i].first;
  }
  if (a[i].second == b[j].first) {
    cnt++;
    same = a[i].second;
  }
  if (a[i].second == b[j].second) {
    cnt++;
    same = a[i].second;
  }
  if (cnt == 1) st1[i].insert(same);
}
void Add2(int i, int j) {
  int cnt = 0, same;
  if (b[i].first == a[j].first) {
    cnt++;
    same = b[i].first;
  }
  if (b[i].first == a[j].second) {
    cnt++;
    same = b[i].first;
  }
  if (b[i].second == a[j].first) {
    cnt++;
    same = b[i].second;
  }
  if (b[i].second == a[j].second) {
    cnt++;
    same = b[i].second;
  }
  if (cnt == 1) st2[i].insert(same);
}
int main() {
  int n, m, i, j, sz;
  cin >> n >> m;
  for (i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
  for (i = 1; i <= m; i++) cin >> b[i].first >> b[i].second;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) Add1(i, j);
  for (i = 1; i <= m; i++)
    for (j = 1; j <= n; j++) Add2(i, j);
  for (i = 1; i <= n; i++) {
    sz = st1[i].size();
    if (sz >= 2) {
      printf("-1\n");
      return 0;
    }
    for (it = st1[i].begin(); it != st1[i].end(); it++) st.insert((*it));
  }
  for (i = 1; i <= m; i++) {
    sz = st2[i].size();
    if (sz >= 2) {
      printf("-1\n");
      return 0;
    }
    for (it = st2[i].begin(); it != st2[i].end(); it++) st.insert((*it));
  }
  sz = st.size();
  if (sz == 1) {
    it = st.begin();
    printf("%d\n", (*it));
    return 0;
  }
  printf("0\n");
  return 0;
}
