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

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);
	for (int i = 0; i < n; i++)
		cin >> a[i];
	int k; 
	cin >> k;
	for (int i = 0; i < n; i++)
		cin >> b[i];

	long long ans = 0;
	
	vector<bool> s(n + 1);
	for (int i = 0; i < k; i++)
		s[b[i]] = true;;
	
	int cur = 0;
	vector<int> answer;
	for (int i = 0; i < n; i++)
	{
        int old = cur;
        while (!s[a[cur]]) cur = (cur + 1) % n;
		ans += (n + cur - old) % n;
        s[a[cur]] = false;
        if (i + k < n) s[b[i + k]] = true;
		answer.push_back(a[cur]);
	}

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