#include <bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
t = 1;
// cin >> t;
while (t--) {
int n;
cin >> n;
int k;
cin >> k;
int A[n];
for (int i = 0; i < n; i++) {
cin >> A[i];
}
int B[k];
for (int i = 0; i < k; i++) {
cin >> B[i];
}
sort(A, A + n);
int f=0;
for(int i=0,j=0;i<n&&j<k;){
if(A[i]>B[j]){
if(i==0){
cout << -1 << endl;
}
else{
cout << A[i-1] << endl;
i=0;
j++;
}
}
else if(A[i]==B[j]){
cout << A[i] << endl;
i=0;
j++;
}
else{
i++;
}
}
cout << f << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGludCBsb25nIGxvbmcgaW50CgppbnQzMl90IG1haW4oKSB7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUobnVsbHB0cik7CgogICAgaW50IHQ7CiAgICB0ID0gMTsKICAgIC8vIGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkgewogICAgICAgIGludCBuOwogICAgICAgIGNpbiA+PiBuOwogICAgICAgIGludCBrOwogICAgICAgIGNpbiA+PiBrOwogICAgICAgIGludCBBW25dOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGNpbiA+PiBBW2ldOwogICAgICAgIH0KICAgICAgICBpbnQgQltrXTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGs7IGkrKykgewogICAgICAgICAgICBjaW4gPj4gQltpXTsKICAgICAgICB9CiAgICAgICAgc29ydChBLCBBICsgbik7CiAgICAgICAgaW50IGY9MDsKICAgICAgICBmb3IoaW50IGk9MCxqPTA7aTxuJiZqPGs7KXsKICAgICAgICAJaWYoQVtpXT5CW2pdKXsKICAgICAgICAJCWlmKGk9PTApewogICAgICAgIAkJCWNvdXQgPDwgLTEgPDwgZW5kbDsKICAgICAgICAJCX0KICAgICAgICAJCWVsc2V7CiAgICAgICAgCQljb3V0IDw8IEFbaS0xXSA8PCBlbmRsOwogICAgICAgIAkJaT0wOwogICAgICAgIAkJaisrOwkKICAgICAgICAJCX0KICAgICAgICAJCQogICAgICAgIAl9CiAgICAgICAgCWVsc2UgaWYoQVtpXT09QltqXSl7CiAgICAgICAgCQljb3V0IDw8IEFbaV0gPDwgZW5kbDsKICAgICAgICAJCWk9MDsKICAgICAgICAJCWorKzsKICAgICAgICAJfQogICAgICAgIAllbHNlewogICAgICAgIAkJaSsrOwogICAgICAgIAl9CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgZiA8PCBlbmRsOwogICAgfQoKICAgIHJldHVybiAwOwp9Cg==