#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
#define int long long
#define ll long long
#define pii std::pair<int, int>
#define pdd std::pair<double, double>
#define INF (1LL << 33)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define SHOW(p) \
    if (test)   \
        cout << #p " : " << p << endl;
bool test = false;
template <class T>
[[maybe_unused]] bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
};
int N, M;
vector<int> A, B;
vector<vector<int>> dp;
signed main()
{
    test = true;
    cin >> N >> M;
    A.resize(N);
    B.resize(M);
    for (auto& v : A) {
        cin >> v;
    }
    for (auto& v : B) {
        cin >> v;
    }
    dp.resize(N + 1);
    REP(i, N + 1)
    {
        dp.at(i).resize(M + 1, 0);
        REP(j, M + 1)
        {
            if (i == 0) {
                dp.at(i).at(j) = j;
            } else if (j == 0) {
                dp.at(i).at(j) = i;
            } else {
                dp.at(i).at(j) = dp.at(i - 1).at(j) + 1;
                chmin(dp.at(i).at(j), dp.at(i).at(j - 1) + 1);
                chmin(dp.at(i).at(j), dp.at(i - 1).at(j - 1) + (A.at(i - 1) == B.at(j - 1) ? 0 : 1));
            }
        }
    }
    cout << dp.at(N).at(M) << endl;

    return 0;
}

#include <bits/stdc++.h>
#define SIZE 200005
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

int A[SIZE];
int L[SIZE],R[SIZE];

int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&A[i]);
		A[i]--;
	}
	set <int> zan,need;
	for(int i=0;i+1<n;i++)
	{
		if(need.find(A[i])!=need.end()) need.erase(A[i]);
		else zan.insert(A[i]);
		if(zan.find(i)!=zan.end()) zan.erase(i);
		else need.insert(i);
		
		if(zan.size()!=1)
		{
			puts("-1");
			return 0;
		}
		L[i]=*zan.begin();
		R[i]=*need.begin();
		//printf("%d %d\n",L[i],R[i]);
	}
	queue <int> que;
	for(int i=0;i<n-1;i++)
	{
		if(A[i]==L[i]&&A[i+1]==R[i])
		{
			que.push(i);
		}
	}
	vector <int> vec;
	while(!que.empty())
	{
		int v=que.front();que.pop();
		vec.push_back(v);
		swap(A[v],A[v+1]);
		if(v>0&&L[v-1]==A[v-1]&&R[v-1]==A[v]) que.push(v-1);
		if(v<n-1&&L[v+1]==A[v+1]&&R[v+1]==A[v+2]) que.push(v+1);
	}
	if(vec.size()<n-1) puts("-1");
	else
	{
		for(int i=0;i<n-1;i++) printf("%d\n",vec[i]+1);
	}
}