#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[50], b[50];
int k, ans, t;
int tot[50];
int c[50];
int aa, bb, cc, dd;
void init() { freopen("d.in", "r", stdin); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d%d", &a[2 * i - 1], &a[2 * i]);
  for (int i = 1; i <= m; i++) scanf("%d%d", &b[2 * i - 1], &b[2 * i]);
  for (int i = 1; i <= n; i++) {
    aa = min(a[2 * i - 1], a[2 * i]);
    bb = max(a[2 * i - 1], a[2 * i]);
    k = 0;
    memset(c, 0, sizeof(c));
    for (int j = 1; j <= m; j++) {
      cc = min(b[2 * j - 1], b[2 * j]);
      dd = max(b[2 * j - 1], b[2 * j]);
      if (aa == cc && bb == dd) continue;
      if (aa == dd && bb == cc) continue;
      if (aa == cc && bb != dd)
        if (!c[aa]) {
          c[aa] = 1;
          t = aa;
          k++;
        }
      if (aa == dd && bb != cc)
        if (!c[aa]) {
          c[aa] = 1;
          t = aa;
          k++;
        }
      if (aa != cc && bb == dd)
        if (!c[bb]) {
          c[bb] = 1;
          t = bb;
          k++;
        }
      if (aa != dd && bb == cc)
        if (!c[bb]) {
          c[bb] = 1;
          t = bb;
          k++;
        }
    }
    if (k > 1) {
      printf("-1");
      return 0;
    }
    if (k == 1)
      if (!tot[t]) {
        tot[t] = 1;
        ans++;
      }
  }
  for (int i = 1; i <= m; i++) {
    aa = min(b[2 * i - 1], b[2 * i]);
    bb = max(b[2 * i - 1], b[2 * i]);
    k = 0;
    memset(c, 0, sizeof(c));
    for (int j = 1; j <= n; j++) {
      cc = min(a[2 * j - 1], a[2 * j]);
      dd = max(a[2 * j - 1], a[2 * j]);
      if (aa == cc && bb == dd) continue;
      if (aa == dd && bb == cc) continue;
      if (aa == cc && bb != dd)
        if (!c[aa]) {
          c[aa] = 1;
          t = aa;
          k++;
        }
      if (aa == dd && bb != cc)
        if (!c[aa]) {
          c[aa] = 1;
          t = aa;
          k++;
        }
      if (aa != cc && bb == dd)
        if (!c[bb]) {
          c[bb] = 1;
          t = bb;
          k++;
        }
      if (aa != dd && bb == cc)
        if (!c[bb]) {
          c[bb] = 1;
          t = bb;
          k++;
        }
    }
    if (k > 1) {
      printf("-1");
      return 0;
    }
    if (k == 1)
      if (!tot[t]) {
        tot[t] = 1;
        ans++;
      }
  }
  if (ans > 1) {
    printf("0");
    return 0;
  }
  for (int i = 1; i <= 9; i++)
    if (tot[i]) printf("%d", i);
  return 0;
}
