import java.util.*;
import java.io.*;

public class dragons_dk {
	BufferedReader br;
	PrintWriter out;
	StringTokenizer stok;

	String nextToken() throws IOException {
		while (stok == null || !stok.hasMoreTokens()) {
			String s = br.readLine();
			if (s == null) {
				return null;
			}
			stok = new StringTokenizer(s);
		}
		return stok.nextToken();
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	char nextChar() throws IOException {
		return (char) (br.read());
	}

	String nextLine() throws IOException {
		return br.readLine();
	}
	
	void solve() throws IOException {
		int k;
		int n = nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		int[] help = new int[n];
		int[] newb = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt() - 1;
			help[a[i]] = i;
		}
		k = nextInt();
		for (int i = 0; i < n; i++) {
			b[i] = nextInt() - 1;
			newb[i] = help[b[i]];
		}
		
		TreeSet<Integer> set = new TreeSet<Integer>();
		for (int i = 0; i < k; i++) {
			set.add(newb[i]);
		}
		
		long ans = 0;
		int current = 0;
		ArrayList<Integer> answer = new ArrayList();
		for (int i = 0; i < n; i++) {
			int q;
			if (set.tailSet(current).isEmpty()) {
				q = set.first();
				
			} else {
				q = set.tailSet(current).first();
			}
			
			ans += (n + q - current) % n;
			current = q;
			
			
			set.remove(q);
			if (i + k < n) {
				set.add(newb[i + k]);
			}
			answer.add(a[q] + 1);
		}
		out.println(ans + n + 60);
		for (int i = 0; i < n - 1; i++) {
			out.print(answer.get(i) + " ");
		}
		out.println(answer.get(n-1));
	}

	void run() throws IOException {
		br = new BufferedReader(new FileReader("dragons.in"));
		out = new PrintWriter("dragons.out");
		// br = new BufferedReader(new FileReader("input.txt"));
		// out = new PrintWriter("output.txt");
		// br = new BufferedReader(new InputStreamReader(System.in));
		// out = new PrintWriter(System.out);
		solve();
		br.close();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		// Localea.setDefault(Locale.US);
		new dragons_dk().run();
	}
}