#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > vt1;
  vector<pair<int, int> > vt2;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    vt1.push_back({a, b});
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    vt2.push_back({a, b});
  }
  int num;
  vector<int> ele;
  for (int i = 0; i < n; i++) {
    int a = vt1[i].first, b = vt1[i].second;
    vector<int> V;
    for (int j = 0; j < m; j++) {
      int temp = 0;
      int x = vt2[j].first, y = vt2[j].second;
      if (a == x || a == y) {
        temp++;
      }
      if (b == x || b == y) {
        temp++;
      }
      if (temp == 1) {
        if (a == x || a == y) num = a;
        if (b == x || b == y) num = b;
        V.push_back(num);
      }
    }
    int l = V.size();
    for (int j = 0; j < l - 1; j++) {
      if (V[j] != V[j + 1]) {
        cout << -1;
        return 0;
      }
    }
    if (l) ele.push_back(num);
  }
  for (int i = 0; i < m; i++) {
    int a = vt2[i].first, b = vt2[i].second;
    vector<int> V;
    for (int j = 0; j < n; j++) {
      int temp = 0;
      int x = vt1[j].first, y = vt1[j].second;
      if (a == x || a == y) {
        temp++;
      }
      if (b == x || b == y) {
        temp++;
      }
      if (temp == 1) {
        if (a == x || a == y) num = a;
        if (b == x || b == y) num = b;
        V.push_back(num);
      }
    }
    int l = V.size();
    for (int j = 0; j < l - 1; j++) {
      if (V[j] != V[j + 1]) {
        cout << -1;
        return 0;
      }
    }
    if (l) ele.push_back(num);
  }
  int len = ele.size();
  for (int i = 0; i < len - 1; i++) {
    if (ele[i] != ele[i + 1]) {
      cout << 0;
      return 0;
    }
  }
  cout << num;
}
