#include <iostream>
using namespace std;
const int MAX = 10001;
void intersection3(int a[MAX], int b[MAX], int result[MAX]) {
int howMany = 0, c[1000000];
for (int i = 1; i <= a[0]; ++i) {
++c[a[i]];
}
for (int i = 1; i <= b[0]; ++i) {
if (c[b[i]] > 0) {
++howMany;
result[howMany] = b[i];
--c[b[i]];
}
}
result[0] = howMany;
for (int i = 0; i <= howMany; ++i) {
cout << result[i];
}
return;
}
int main() {
int a[] = {5, 1, 2, 2, 5, 8}, b[] = {6, 6, 6, 6, 6, 6, 6}, result[MAX];
intersection3(a, b, result);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1BWCA9IDEwMDAxOwoKdm9pZCBpbnRlcnNlY3Rpb24zKGludCBhW01BWF0sIGludCBiW01BWF0sIGludCByZXN1bHRbTUFYXSkgewoJaW50IGhvd01hbnkgPSAwLCBjWzEwMDAwMDBdOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gYVswXTsgKytpKSB7CiAgICAJKytjW2FbaV1dOwogICAgfQogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gYlswXTsgKytpKSB7CiAgICAJaWYgKGNbYltpXV0gPiAwKSB7CiAgICAJCSsraG93TWFueTsKICAgIAkJcmVzdWx0W2hvd01hbnldID0gYltpXTsgCiAgICAJCS0tY1tiW2ldXTsKICAgIAl9CiAgICB9CiAgICByZXN1bHRbMF0gPSBob3dNYW55OwogICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gaG93TWFueTsgKytpKSB7CiAgICAJY291dCA8PCByZXN1bHRbaV07CiAgICB9CiAgICByZXR1cm47Cn0KaW50IG1haW4oKSB7CiAgICBpbnQgYVtdID0gezUsIDEsIDIsIDIsIDUsIDh9LCBiW10gPSB7NiwgNiwgNiwgNiwgNiwgNiwgNn0sIHJlc3VsdFtNQVhdOwogICAgaW50ZXJzZWN0aW9uMyhhLCBiLCByZXN1bHQpOwogICAgcmV0dXJuIDA7Cn0K