#include <bits/stdc++.h>
using namespace std;
const int max_n = 15, inf = 1000111222;
vector<pair<int, int> > a, b;
vector<int> other_a;
vector<int> other_b;
int can(pair<int, int> pa, pair<int, int> pb) {
  if (pa.first == pb.first && pa.second != pb.second) return pa.first;
  if (pa.second == pb.second && pa.first != pb.first) return pa.second;
  if (pa.first == pb.second && pa.second != pb.first) return pa.first;
  if (pa.second == pb.first && pa.first != pb.second) return pa.second;
  return 0;
}
int main() {
  int la, lb;
  cin >> la >> lb;
  pair<int, int> p;
  for (int i = 0; i < la; ++i) {
    cin >> p.first >> p.second;
    if (p.first > p.second) {
      swap(p.first, p.second);
    }
    a.push_back(p);
  }
  for (int i = 0; i < lb; ++i) {
    cin >> p.first >> p.second;
    if (p.first > p.second) {
      swap(p.first, p.second);
    }
    b.push_back(p);
  }
  set<int> c;
  for (int i = 0; i < la; ++i) {
    for (int j = 0; j < lb; ++j) {
      int comm = can(a[i], b[j]);
      if (comm != 0) {
        c.insert(comm);
      }
    }
  }
  if (c.size() == 0) {
    exit(228);
  }
  if (c.size() == 1) {
    cout << *c.begin();
    return 0;
  }
  for (int i = 0; i < la; ++i) {
    set<int> c;
    for (int j = 0; j < lb; ++j) {
      int comm = can(a[i], b[j]);
      if (comm != 0) {
        c.insert(comm);
      }
    }
    if (c.size() > 1) {
      cout << -1;
      return 0;
    }
  }
  for (int i = 0; i < lb; ++i) {
    set<int> c;
    for (int j = 0; j < la; ++j) {
      int comm = can(b[i], a[j]);
      if (comm != 0) {
        c.insert(comm);
      }
    }
    if (c.size() > 1) {
      cout << -1;
      return 0;
    }
  }
  cout << 0;
  return 0;
}
