#include <iostream>
#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
	freopen("dragons.in", "r", stdin);
	freopen("dragons.out", "w", stdout);
	int n;
	cin >> n;
	vector<int> a(n), b(n), d(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
		d[a[i]] = i;
	}
	int k; 
	cin >> k;
	for (int i = 0; i < n; i++)
	{
		cin >> b[i];
		b[i]--;
	}

	vector<int> c(n);
	for (int i = 0; i < n; i++)
		c[i] = d[b[i]];
	
	long long ans = 0;
	
	set<int> s;
	for (int i = 0; i < k; i++)
		s.insert(c[i]);
	
	int cur = 0;
	vector<int> answer;
	for (int i = 0; i < n; i++)
	{
		set<int>::iterator it = s.upper_bound(cur - 1);
		int ne = *s.begin();
		if (it != s.end()) ne = *s.begin();
		ans += (n + ne - cur) % n;
		cur = ne;
		s.erase(ne);
		if (i + k < n) s.insert(c[i + k]);
		answer.push_back(ne);
	}

	cout << ans + 60 + n << endl;
	for (int i = 0; i < n; i++)
		cout << a[answer[i]] + 1 << ((i == n - 1) ? "\n" : " ");
}
