#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;
int n, m, a, b;
vector<pair<int, int> > x, y;
array<set<int>, 15> out1, out2;
int edge(const pair<int, int> &a, const pair<int, int> &b) {
  int c, d, e, f;
  c = min(a.first, a.second);
  d = max(a.first, a.second);
  e = min(b.first, b.second);
  f = max(b.first, b.second);
  int x = c + d + e + f;
  set<int> s;
  s.insert(c);
  s.insert(d);
  s.insert(e);
  s.insert(f);
  int y = 0;
  for (int z : s) y += z;
  if (((c == e) && (d != f)) || ((d == f) && (c != e)) || (d == e) || (c == f))
    return x - y;
  return -1;
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    x.push_back(make_pair(a, b));
  }
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    y.push_back(make_pair(a, b));
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int z = edge(x[i], y[j]);
      if (z != -1) {
        out1[i].insert(z);
        out2[j].insert(z);
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += out1[i].size();
  }
  for (int i = 0; i < m; i++) {
    cnt += out2[i].size();
  }
  if (cnt == 2) {
    for (int i = 0; i < n; i++) {
      if (out1[i].size() > 0) {
        cout << *out1[i].begin();
        break;
      }
    }
    return 0;
  }
  bool multi = false;
  for (int i = 0; i < n; i++) {
    if (out1[i].size() > 1) multi = true;
  }
  for (int i = 0; i < m; i++) {
    if (out2[i].size() > 1) multi = true;
  }
  if (multi)
    cout << "-1";
  else {
    set<int> ans;
    for (int i = 0; i < n; i++) {
      ans.insert(out1[i].begin(), out1[i].end());
    }
    for (int i = 0; i < m; i++) {
      ans.insert(out2[i].begin(), out2[i].end());
    }
    if (ans.size() == 1)
      cout << *ans.begin();
    else
      cout << "0";
  }
  return 0;
}
