#include <bits/stdc++.h>
using namespace std;
int n, m, x = -1, l = 0;
int p1[15][2];
int p2[15][2];
bool o[10];
vector<pair<int, int> > cand;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> p1[i][0] >> p1[i][1];
  for (int i = 0; i < m; i++) cin >> p2[i][0] >> p2[i][1];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (p1[i][0] == p2[j][0] && p1[i][1] != p2[j][1]) {
        x = p1[i][0];
        o[p1[i][0]] = 1;
        cand.push_back(make_pair(i, j));
      } else if (p1[i][0] == p2[j][1] && p1[i][1] != p2[j][0]) {
        x = p1[i][0];
        o[p1[i][0]] = 1;
        cand.push_back(make_pair(i, j));
      } else if (p1[i][0] != p2[j][0] && p1[i][1] == p2[j][1]) {
        x = p1[i][1];
        o[p1[i][1]] = 1;
        cand.push_back(make_pair(i, j));
      } else if (p1[i][0] != p2[j][1] && p1[i][1] == p2[j][0]) {
        x = p1[i][1];
        o[p1[i][1]] = 1;
        cand.push_back(make_pair(i, j));
      }
    }
  }
  for (int i = 1; i <= 9; i++) l += o[i];
  if (l == 1) {
    cout << x;
    return 0;
  }
  for (int i = 0; i < cand.size(); i++) {
    int a = p1[cand[i].first][0];
    int b = p1[cand[i].first][1];
    int s = 0;
    for (int j = 1; j <= 9; j++) o[j] = 0;
    for (int j = 0; j < m; j++) {
      if (a + b != p2[j][0] + p2[j][1] && (a == p2[j][0] || a == p2[j][1]))
        o[a] = 1;
      else if (a + b != p2[j][0] + p2[j][1] && (b == p2[j][0] || b == p2[j][1]))
        o[b] = 1;
    }
    for (int j = 1; j <= 9; j++) s += o[j];
    if (s != 1) {
      cout << -1;
      return 0;
    }
    a = p2[cand[i].second][0];
    b = p2[cand[i].second][1];
    s = 0;
    for (int j = 1; j <= 9; j++) o[j] = 0;
    for (int j = 0; j < n; j++) {
      if (a + b != p1[j][0] + p1[j][1] && (a == p1[j][0] || a == p1[j][1]))
        o[a] = 1;
      else if (a + b != p1[j][0] + p1[j][1] && (b == p1[j][0] || b == p1[j][1]))
        o[b] = 1;
    }
    for (int j = 1; j <= 9; j++) s += o[j];
    if (s != 1) {
      cout << -1;
      return 0;
    }
  }
  cout << 0;
  return 0;
}
