#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
const int INF = 1e9 + 1;
using namespace std;
vector<bool> used;
vector<vector<array<long long, 2>>> g;
bool dfs(int v, int t) {
  used[v] = true;
  for (auto u : g[v]) {
    if (t == -1) t = u[1];
    if (t != u[1]) return false;
    if (!used[u[0]])
      if (!dfs(u[0], u[1])) return false;
  }
  return true;
}
int main() {
  std::ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<array<long long, 2>> a(n), b(m);
  for (long long i = 0; i < (n); i++) {
    cin >> a[i][0] >> a[i][1];
    if (a[i][0] > a[i][1]) swap(a[i][0], a[i][1]);
  }
  for (long long i = 0; i < (m); i++) {
    cin >> b[i][0] >> b[i][1];
    if (b[i][0] > b[i][1]) swap(b[i][0], b[i][1]);
  }
  g.resize(n + m);
  for (long long i = 0; i < (n); i++) {
    for (int j = (0); j < (m); j++) {
      if (a[i] == b[j]) continue;
      if (a[i][0] == b[j][0])
        g[i].push_back({n + j, a[i][0]}), g[n + j].push_back({i, a[i][0]});
      if (a[i][0] == b[j][1])
        g[i].push_back({n + j, a[i][0]}), g[n + j].push_back({i, a[i][0]});
      if (a[i][1] == b[j][0])
        g[i].push_back({n + j, a[i][1]}), g[n + j].push_back({i, a[i][1]});
      if (a[i][1] == b[j][1])
        g[i].push_back({n + j, a[i][1]}), g[n + j].push_back({i, a[i][1]});
    }
  }
  used.resize(n + m);
  set<int> answ;
  for (long long i = 0; i < (n + m); i++) {
    if (used[i]) continue;
    if (!dfs(i, -1)) return cout << -1 << endl, 0;
    if (g[i].size() > 0) answ.insert(g[i][0][1]);
  }
  if (answ.size() == 1)
    cout << *answ.begin() << endl;
  else
    cout << 0 << endl;
  return 0;
}
