#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, f = 0, ans, m, a1[20], a2[20], b1[20], b2[20], flag = 0, num[20] = {0};
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a1[i] >> a2[i];
  for (int i = 0; i < m; i++) cin >> b1[i] >> b2[i];
  for (int i = 0; i < n; i++) {
    int cura = 0, curb = 0;
    for (int j = 0; j < m; j++) {
      if (a1[i] == b1[j] && a2[i] != b2[j]) num[a1[i]]++, cura++;
      if (a1[i] == b2[j] && a2[i] != b1[j]) num[a1[i]]++, cura++;
      if (a2[i] == b1[j] && a1[i] != b2[j]) num[a2[i]]++, curb++;
      if (a2[i] == b2[j] && a1[i] != b1[j]) num[a2[i]]++, curb++;
    }
    if (cura > 0 && curb > 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < m; i++) {
    int cura = 0, curb = 0;
    for (int j = 0; j < n; j++) {
      if (b1[i] == a1[j] && b2[i] != a2[j]) cura++;
      if (b1[i] == a2[j] && b2[i] != a1[j]) cura++;
      if (b2[i] == a1[j] && b1[i] != a2[j]) curb++;
      if (b2[i] == a2[j] && b1[i] != a1[j]) curb++;
    }
    if (cura > 0 && curb > 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  int c = 0;
  for (int i = 0; i < 15; i++) {
    if (num[i] > 0) {
      c++;
      f = i;
    }
  }
  if (c > 1)
    cout << 0 << endl;
  else
    cout << f << endl;
  return 0;
}
