#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> p[20], q[20];
bool pcan = true, qcan = true;
set<int> sx;
int com(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first and a.second != b.second)
    return a.first;
  else if (a.first == b.second and a.second != b.first)
    return a.first;
  else if (a.second == b.second and a.first != b.first)
    return a.second;
  else if (a.second == b.first and a.first != b.second)
    return a.second;
  return -1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second;
  for (int i = 0; i < m; i++) cin >> q[i].first >> q[i].second;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int x = com(p[i], q[j]);
      if (x != -1) {
        sx.insert(x);
        for (int J = 0; J < m; J++) {
          int y = com(p[i], q[J]);
          if (y != x and y != -1) pcan = false;
        }
        for (int I = 0; I < n; I++) {
          int y = com(p[I], q[j]);
          if (y != x and y != -1) qcan = false;
        }
      }
    }
  if (sx.size() == 1)
    cout << *sx.begin() << endl;
  else if (pcan and qcan)
    cout << 0 << endl;
  else
    cout << -1 << endl;
  return 0;
}
