#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const long double pi = acos(-1);
const int MOD = 1e9 + 7;
const int N = 1e6;
pair<int, int> a[20], b[20];
int n, m;
vector<int> g[22], g2[22];
set<int> duplas[21];
set<int> get_inter(set<int> A, set<int> B) {
  set<int> ret;
  for (int x : A)
    if (B.count(x)) ret.insert(x);
  return ret;
}
bool inter(int i, int j) {
  set<int> A = {a[i].first, a[i].second};
  set<int> B = {b[j].first, b[j].second};
  int num = 0;
  for (int x : A)
    if (B.count(x)) num++;
  return num == 1;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a[i].first, &a[i].second);
    if (a[i].first > a[i].second) swap(a[i].first, a[i].second);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &b[i].first, &b[i].second);
    if (b[i].first > b[i].second) swap(b[i].first, b[i].second);
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (inter(i, j)) {
        g[i].push_back(j), g2[j].push_back(i);
      }
  set<int> caras;
  for (int i = 0; i < n; i++) {
    set<int> num;
    set<int> A = {a[i].first, a[i].second};
    for (int j : g[i]) {
      set<int> B = {b[j].first, b[j].second};
      set<int> inter = get_inter(A, B);
      for (int x : inter) num.insert(x);
    }
    if (num.size() > 1) {
      printf("%d\n", -1);
      return 0;
    }
    for (int x : num) caras.insert(x);
  }
  set<int> caras2;
  for (int i = 0; i < m; i++) {
    set<int> num;
    set<int> A = {b[i].first, b[i].second};
    for (int j : g2[i]) {
      set<int> B = {a[j].first, a[j].second};
      set<int> inter = get_inter(A, B);
      for (int x : inter) num.insert(x);
    }
    if (num.size() > 1) {
      printf("%d\n", -1);
      return 0;
    }
    for (int x : num) caras2.insert(x);
  }
  if (caras.size() > 1 or caras2.size() > 1) {
    printf("%d\n", 0);
    return 0;
  }
  printf("%d\n", *(caras.begin()));
  return 0;
}
