#include <bits/stdc++.h>
using namespace std;
bool check1(pair<int, int> x, pair<int, int> x_) {
  return x.first == x_.first || x.first == x_.second;
}
bool check2(pair<int, int> x, pair<int, int> x_) {
  return x.second == x_.first || x.second == x_.second;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > aRr1, aRr2;
  for (int i = 0; i < n; ++i) {
    static pair<int, int> p;
    cin >> p.first >> p.second;
    aRr1.push_back(p);
  }
  for (int i = 0; i < m; ++i) {
    static pair<int, int> p;
    cin >> p.first >> p.second;
    aRr2.push_back(p);
  }
  int ans, ans_;
  vector<int> aRr1_(10), aRr2_(10);
  for (int i = 0; i < n; ++i) {
    static unsigned int count1, count2;
    count1 = 0;
    count2 = 0;
    for (int j = 0; j < m; ++j) {
      if (check1(aRr1[i], aRr2[j])) {
        if (!check2(aRr1[i], aRr2[j])) ++count1;
      } else {
        if (check2(aRr1[i], aRr2[j])) ++count2;
      }
    }
    if (count1)
      if (count2) {
        cout << -1;
        return 0;
      } else
        ++aRr1_[aRr1[i].first];
    else if (count2)
      ++aRr1_[aRr1[i].second];
  }
  for (int i = 0; i < m; ++i) {
    static unsigned int count1, count2;
    count1 = 0;
    count2 = 0;
    for (int j = 0; j < n; ++j) {
      if (check1(aRr2[i], aRr1[j])) {
        if (!check2(aRr2[i], aRr1[j])) ++count1;
      } else {
        if (check2(aRr2[i], aRr1[j])) ++count2;
      }
    }
    if (count1)
      if (count2) {
        cout << -1;
        return 0;
      } else
        ++aRr2_[aRr2[i].first];
    else if (count2)
      ++aRr2_[aRr2[i].second];
  }
  ans = 0;
  for (int i = 1; i < 10; ++i)
    if (aRr1_[i]) {
      ++ans;
      ans_ = i;
    }
  if (ans > 1) {
    cout << 0;
    return 0;
  }
  cout << ans_;
}
