#include <bits/stdc++.h>
using namespace std;
int common(vector<int> a, vector<int> b) {
  if (a[0] == b[0] || a[0] == b[1]) return a[0];
  if (a[1] == b[0] || a[1] == b[1]) return a[1];
  return 0;
}
void no() {
  printf("-1\n");
  exit(0);
}
vector<vector<int>> A;
vector<vector<int>> B;
int n, m;
set<int> calc() {
  set<int> totcs;
  for (int i = 0; i < n; ++i) {
    int ok = 0;
    set<int> cs;
    for (int j = 0; j < m; ++j) {
      if (B[j] == A[i]) continue;
      int c = common(A[i], B[j]);
      if (!c) continue;
      cs.insert(c);
    }
    if (cs.size() > 1) no();
    if (cs.empty()) continue;
    totcs.insert(*cs.begin());
  }
  return totcs;
}
int main() {
  scanf("%d%d", &n, &m);
  A.resize(n);
  B.resize(m);
  for (int i = 0; i < n; ++i) {
    A[i].resize(2);
    scanf("%d%d", &A[i][0], &A[i][1]);
    if (A[i][0] > A[i][1]) swap(A[i][0], A[i][1]);
  }
  for (int i = 0; i < m; ++i) {
    B[i].resize(2);
    scanf("%d%d", &B[i][0], &B[i][1]);
    if (B[i][0] > B[i][1]) swap(B[i][0], B[i][1]);
  }
  auto as = calc();
  swap(n, m);
  swap(A, B);
  auto bs = calc();
  set<int> inter;
  for (int a : as) {
    if (bs.count(a)) inter.insert(a);
  }
  if (inter.size() == 1)
    printf("%d\n", *inter.begin());
  else
    printf("0\n");
}
