#include <bits/stdc++.h>
using namespace std;
int n, m, p[4][20][4];
set<int> s, s2[4][20];
vector<int> v;
void solve() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> p[1][i][1] >> p[1][i][2];
  }
  for (int i = 1; i <= m; i++) {
    cin >> p[2][i][1] >> p[2][i][2];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      v.clear();
      for (int x = 1; x <= 2; x++) {
        for (int y = 1; y <= 2; y++) {
          if (p[1][i][x] == p[2][j][y]) v.push_back(p[1][i][x]);
        }
      }
      if (v.size() == 1) {
        s.insert(v[0]);
        s2[1][i].insert(v[0]);
        s2[2][j].insert(v[0]);
      }
    }
  }
  if (s.size() == 1)
    cout << (*(s.begin())) << "\n";
  else {
    for (int i = 1; i <= n; i++) {
      if (s2[1][i].size() > 1) {
        cout << "-1\n";
        return;
      }
    }
    for (int i = 1; i <= m; i++) {
      if (s2[2][i].size() > 1) {
        cout << "-1\n";
        return;
      }
    }
    cout << "0\n";
  }
}
int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
