#include <bits/stdc++.h>
using namespace std;
const int MAX = 11;
int match(pair<int, int> p1, pair<int, int> p2) {
  if (p1.first == p2.first && p1.second == p2.second) return -1;
  if (p1.first == p2.second && p1.second == p2.first) return -1;
  if (p1.first == p2.first) return p1.first;
  if (p1.first == p2.second) return p1.first;
  if (p2.first == p1.second) return p1.second;
  if (p1.second == p2.second) return p1.second;
  return -1;
}
bool operator==(pair<int, int> p, pair<int, int> q) {
  if (p.first == q.first && p.second == q.second) return true;
  if (p.second == q.first && p.first == q.second) return true;
  return false;
}
bool isAmbiguous(pair<int, int> p, pair<int, int> q1, pair<int, int> q2) {
  if (p == q1) return false;
  if (p == q2) return false;
  if (q1 == q2) return false;
  int temp = -1;
  if (p.first == q1.first || p.first == q1.second) {
    temp = 1;
  } else if (p.first == q2.second || p.first == q2.first) {
    temp = 2;
  }
  if (temp == 1 && (p.second == q2.first || p.second == q2.second)) {
    return true;
  } else if (temp == 2 && (p.second == q1.first || p.second == q1.second)) {
    return true;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > p1, p2;
  bool mt[MAX] = {0};
  int count = 0, cand = 0;
  int k1, k2;
  for (int i = 0; i < n; i++) {
    cin >> k1 >> k2;
    p1.push_back(make_pair(k1, k2));
  }
  for (int i = 0; i < m; i++) {
    cin >> k1 >> k2;
    p2.push_back(make_pair(k1, k2));
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int temp = match(p1[i], p2[j]);
      if (temp > -1 && mt[temp] == 0) {
        mt[temp] = 1;
        count++;
        cand = temp;
      }
    }
  }
  if (count == 1) {
    cout << cand << "\n";
    return 0;
  }
  for (int i = 0; i < MAX; i++) {
    mt[i] = 0;
    count = 0;
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      int temp = match(p2[i], p1[j]);
      if (temp > -1 && mt[temp] == 0) {
        mt[temp] = 1;
        count++;
        cand = temp;
      }
    }
  }
  if (count == 1) {
    cout << cand << "\n";
    return 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < m; k++) {
        if (j != k && isAmbiguous(p1[i], p2[j], p2[k])) {
          cout << -1 << "\n";
          return 0;
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (j != k && isAmbiguous(p2[i], p1[j], p1[k])) {
          cout << -1 << "\n";
          return 0;
        }
      }
    }
  }
  cout << 0 << "\n";
  return 0;
}
