#include <bits/stdc++.h>
using namespace std;
set<int> SET;
vector<pair<int, int> > v1;
vector<pair<int, int> > v2;
int A[105];
int B[105];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int a, b;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    if (a > b) swap(a, b);
    v1.push_back(make_pair(a, b));
  }
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &a, &b);
    if (a > b) swap(a, b);
    v2.push_back(make_pair(a, b));
  }
  for (int i = 0; i < n; i++) {
    bool match1 = false;
    bool match2 = false;
    for (int j = 0; j < m; j++) {
      if (v1[i] == v2[j]) continue;
      if (v1[i].first == v2[j].first || v1[i].first == v2[j].second) {
        match1 = true;
        SET.insert(v1[i].first);
      }
      if (v1[i].second == v2[j].first || v1[i].second == v2[j].second) {
        match2 = true;
        SET.insert(v1[i].second);
      }
    }
    A[i] = match1 + match2;
  }
  for (int i = 0; i < m; i++) {
    bool match1 = false;
    bool match2 = false;
    for (int j = 0; j < n; j++) {
      if (v2[i] == v1[j]) continue;
      if (v2[i].first == v1[j].first || v2[i].first == v1[j].second) {
        match1 = true;
        SET.insert(v2[i].first);
      }
      if (v2[i].second == v1[j].first || v2[i].second == v1[j].second) {
        match2 = true;
        SET.insert(v2[i].second);
      }
    }
    B[i] = match1 + match2;
  }
  for (int i = 0; i < n; i++)
    if (A[i] == 2) {
      printf("-1");
      return 0;
    }
  for (int i = 0; i < m; i++)
    if (B[i] == 2) {
      printf("-1");
      return 0;
    }
  if (SET.size() > 1) {
    printf("0");
    return 0;
  }
  printf("%d\n", *SET.begin());
}
