#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, const array<T, 2>& a) {
  for (T e : a) {
    os << e << ' ';
  };
  return os;
}
inline void lol(const function<void(int)>& fun, const array<int, 2>& f,
                const array<int, 2>& s) {
  if (f == s) {
    return;
  }
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      if (f[i] == s[j]) {
        fun(f[i]);
      }
    }
  }
}
inline int common(const array<int, 2>& f, const array<int, 2>& s) {
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      if (f[i] == s[j]) {
        return f[i];
      }
    }
  }
}
bool ok(const vector<array<int, 2>>& a, const array<int, 2>& f) {
  unordered_set<int> s;
  for (auto& p : a) {
    lol([&](int x) { s.emplace(x); }, p, f);
  }
  return s.size() == 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<array<int, 2>> first(n), second(m);
  for (int i = 0; i < n; ++i) {
    cin >> first[i][0] >> first[i][1];
    if (first[i][0] > first[i][1]) {
      swap(first[i][0], first[i][1]);
    }
  }
  for (int i = 0; i < m; ++i) {
    cin >> second[i][0] >> second[i][1];
    if (second[i][0] > second[i][1]) {
      swap(second[i][0], second[i][1]);
    }
  }
  unordered_set<int> candidates;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      lol([&](int x) { candidates.emplace(x); }, first[i], second[j]);
    }
  }
  if (candidates.size() == 1) {
    cout << *candidates.begin() << '\n';
    return 0;
  }
  bool they_know = true;
  for (auto& f : first) {
    vector<array<int, 2>> v;
    for (auto& s : second) {
      lol([&](int x) { v.emplace_back(s); }, f, s);
    }
    if (v.empty()) {
      continue;
    }
    int chosen = common(f, v.front());
    for (int i = 1; i < v.size(); ++i) {
      if (chosen != common(f, v[i])) {
        they_know = false;
        break;
      }
    }
    for (int i = 0; i < v.size(); ++i) {
      if (!ok(first, v[i])) {
        they_know = false;
        break;
      }
    }
    if (!they_know) {
      break;
    }
  }
  if (they_know) {
    cout << "0\n";
  } else {
    cout << "-1\n";
  }
  return 0;
}
