#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;
int read() {
int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}
const int N = 5010;
const int M = 50010;
int n, m, cnt;
int a[N], b[N], pr[N], pos[N], pt[N];
int p[100][100];
int ans[N];
namespace F { // max_flow_with_cost
const int inf = 0x3f3f3f3f;
int h[N], now[N], lst[N], cnt = 1, S, T;
bool vis[N], ban[N];
ll maxflow, cost, dis[N];
struct edge { int v, w, c, nxt; } e[M << 1];
inline void tlink(int x, int y, int w, int c) { e[++cnt] = (edge) { y, w, c, h[x]}; h[x] = cnt; }
inline void link(int x, int y, int w, int c) { tlink(x, y, w, c); tlink(y, x, 0, -c); }
inline void setst(int s, int t) { S = s; T = t; }
bool bfs(int s = S) {
queue < int > q; q.push(s); rep(i, S, T) dis[i] = -1e9;
memset(vis, 0, sizeof vis); dis[s] = 0; now[s] = h[s]; vis[s] = 1;
memset(lst, -1, sizeof(lst));
/*if(s) {
rep(i, 1, 2 * m) for (int j = h[i]; j; j = e[j].nxt)
if(e[j].v > m && !e[j].w) cout << i << ' ' << e[j].v << '\n';
cout << '\n';
}*/
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i = h[x], y; i; i = e[i].nxt)
if(e[i].w && dis[y = e[i].v] < dis[x] + e[i].c && !ban[y]) {
dis[y] = dis[x] + e[i].c, lst[y] = (i ^ 1); now[y] = h[y];
if(!vis[y]) vis[y] = 1, q.push(y);
}
vis[x] = 0;
}
/*if(s) {
cout << s << '\n';
rep(i, S, T) cout << i << ' ' << dis[i] << ' ' << lst[i] << '\n'; cout << '\n';
}*/
return dis[T] > -1e9;
}
int dinic(int x,int flow,ll c) {
if(x == T) return maxflow += flow, cost += c * flow, flow;
int res = flow; vis[x] = 1;
for(int i = now[x], y; i && res; i = e[i].nxt) {
now[x] = i;
if(e[i].w && dis[y = e[i].v] == dis[x] + e[i].c && !vis[y]) {
int k = dinic(y, min(res,e[i].w), c + e[i].c);
e[i].w -= k; e[i ^ 1].w += k; res -= k;
}
} vis[x] = 0;
return flow - res;
}
void runit() {
while(bfs()) dinic(S, inf, 0);
rep(i, 1, m) for (int j = h[i]; j; j = e[j].nxt)
if(!e[j].w) ans[i] = e[j].v - m;
// rep(i, 1, m) cout << ans[i] << ' '; cout << '\n';
rep(i, 1, m) assert(ans[i]);
rep(t, 1, m) {
int i = pr[t]; if(!i) break ;
rep(j, 1, ans[i] - 1) {
bfs(j + m);
// cout << i << ' ' << j << ' ' << dis[i] << ' ' << p[i][j] << '\n';
if(dis[i] + p[i][j] != 0) continue ;
// cout << i << ' ' << j << ' ' << dis[i] << ' ' << p[i][j] << '\n';
// rep(k, 1, 2 * m) cout << k << ' ' << e[lst[k]].v << '\n'; cout << '\n';
auto upd = [&](int x) {
e[x].w ^= 1, e[x ^ 1].w ^= 1;
} ;
int k = i;
for ( ; e[lst[k]].v != j + m; k = e[lst[k]].v) {
// cout << k << ' ' << e[lst[k]].v << ' ' << e[lst[e[lst[k]].v]].v << '\n';
upd(lst[k]);
k = e[lst[k]].v, ans[e[lst[k]].v] = k - m;
upd(lst[k]);
}
upd(lst[k]);
for (k = h[i]; k; k = e[k].nxt) if(e[k].v == j + m) {
upd(k); break ;
}
ans[i] = j;
// rep(k, 1, m) cout << ans[k] << ' '; cout << '\n';
}
ban[i] = 1;
}
}
}
int main() {
n = in, m = in; rep(i, 1, n) a[i] = in; rep(i, 1, n) b[i] = in;
rep(i, 1, n) if(!pos[a[i]]) pos[a[i]] = i, pr[++cnt] = a[i];
rep(i, 1, m) if(!pos[i]) pos[i] = n + 1;
rep(i, 1, m) pt[i] = i;
sort(pt + 1, pt + m + 1, [&](int x, int y) { return pos[x] > pos[y]; });
rep(i, 1, m) pos[pt[i]] = i;
rep(i, 1, n) p[a[i]][b[i]]++;
F :: setst(0, m * 2 + 1);
rep(i, 1, m) F :: link(0, i, 1, 0), F :: link(i + m, m * 2 + 1, 1, 0);
rep(i, 1, m) rep(j, 1, m) F :: link(i, j + m, 1, p[i][j]);
F :: runit();
rep(i, 1, n) printf("%d ", ans[a[i]]); printf("\n");
}