#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, x, y;
  cin >> n >> m;
  pair<int, int> a[n], b[m];
  bool p1[10], p2[10];
  for (int i = 0; i < 10; i++) {
    p1[i] = 0;
    p2[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    a[i] = make_pair(x, y);
    p1[x] = 1;
    p1[y] = 1;
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    b[i] = make_pair(x, y);
    p2[x] = 1;
    p2[y] = 1;
  }
  bool b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0;
  int u, v;
  for (int i = 0; i < n; i++) {
    b1 = 0;
    b2 = 0;
    for (int j = 0; j < m; j++) {
      if (a[i].first != b[j].first && a[i].second == b[j].second) {
        if (b1 == 0) {
          if (b5 == 1 && u != a[i].second) {
            b4 = 1;
          }
          b1 = 1;
          b5 = 1;
          u = a[i].second;
        } else if (b1 == 1 && p2[a[i].first] == 1) {
          b2 = 1;
        }
      }
      if (a[i].first == b[j].first && a[i].second != b[j].second) {
        if (b1 == 0) {
          if (b5 == 1 && u != a[i].first) {
            b4 = 1;
          }
          b1 = 1;
          b5 = 1;
          u = a[i].first;
          v = i;
        } else if (b1 == 1 && p2[a[i].second] == 1) {
          b2 = 1;
        }
      }
      if (a[i].first == b[j].second && a[i].second != b[j].first) {
        if (b1 == 0) {
          if (b5 == 1 && u != a[i].first) {
            b4 = 1;
          }
          b1 = 1;
          b5 = 1;
          u = a[i].first;
          v = i;
        } else if (b1 == 1 && p2[a[i].second] == 1) {
          b2 = 1;
        }
      }
      if (a[i].first != b[j].second && a[i].second == b[j].first) {
        if (b1 == 0) {
          if (b5 == 1 && u != a[i].second) {
            b4 = 1;
          }
          b1 = 1;
          b5 = 1;
          u = a[i].second;
          v = i;
        } else if (b1 == 1 && p2[a[i].first] == 1) {
          b2 = 1;
        }
      }
    }
    if (b2 == 1) {
      break;
    }
  }
  b3 = b2;
  b1 = 0;
  b2 = 0;
  b5 = 0;
  for (int i = 0; i < m; i++) {
    b1 = 0;
    b2 = 0;
    for (int j = 0; j < n; j++) {
      if (a[j].first != b[i].first && a[j].second == b[i].second) {
        if (b1 == 0) {
          if (b5 == 1 && u != a[j].second) {
            b4 = 1;
          }
          b1 = 1;
          b5 = 1;
          u = a[j].second;
          v = i;
        } else if (b1 == 1 && p1[b[i].first] == 1) {
          b2 = 1;
        }
      }
      if (a[j].first == b[i].first && a[j].second != b[i].second) {
        if (b1 == 0) {
          if (b5 == 1 && u != a[j].first) {
            b4 = 1;
          }
          b1 = 1;
          b5 = 1;
          u = a[j].first;
          v = i;
        } else if (b1 == 1 && p1[b[i].second] == 1) {
          b2 = 1;
        }
      }
      if (a[j].first == b[i].second && a[j].second != b[i].first) {
        if (b1 == 0) {
          if (b5 == 1 && u != a[j].first) {
            b4 = 1;
          }
          b1 = 1;
          b5 = 1;
          u = a[j].first;
          v = i;
        } else if (b1 == 1 && p1[b[i].first] == 1) {
          b2 = 1;
        }
      }
      if (a[j].first != b[i].second && a[j].second == b[i].first) {
        if (b1 == 0) {
          if (b5 == 1 && u != a[j].second) {
            b4 = 1;
          }
          b1 = 1;
          b5 = 1;
          u = a[j].second;
          v = i;
        } else if (b1 == 1 && p1[b[i].second] == 1) {
          b2 = 1;
        }
      }
    }
    if (b2 == 1) {
      break;
    }
  }
  b3 = max(b2, b3);
  if (b3 == 1) {
    cout << -1 << "\n";
  } else if (b3 == 0 && b4 == 1) {
    cout << 0 << "\n";
  } else {
    cout << u << "\n";
  }
  return 0;
}
