#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MaxN 5001
long n, k, a [MaxN], s [2 * MaxN];

void Swap (long k)
{
	long i, tmp;

	for (i = 1; i <= k / 2; i++)
	{
		tmp = a [i];
		a [i] = a [k + 1 - i];
		a [k + 1 - i] = tmp;
	}
}

void End ()
{
   FILE *sc;
        sc = fopen("tmp.txt", "w");
	fprintf(sc, "0\n");
        fclose(sc);
	printf("0\n");
	exit (0);
}

int main (int argc, char *argv[])
{
	FILE *in, *out, *sol, *sc;
	long i, tmp, ok;

	in = fopen (argv [1], "r");
	out = fopen (argv [2], "r");
	sol = fopen (argv [3], "r");
	if ((in == NULL) || (out == NULL) || (sol == NULL)) End();

	fscanf (in, "%ld", &n);
	for (i = 1; i <= n; i++)
		fscanf (in, "%ld", &a [i]);
	fscanf (out, "%ld", &k);
	if (k >= 2 * n) End();

	for (i = 1; i <= k; i++)
	{
		if (fscanf (out, "%ld", &tmp) == EOF)
			End ();
		if ((tmp < 1) || (tmp > n))
			End ();
		Swap (tmp);
	}
	if (fscanf (out, "%ld", &tmp) != EOF)
		End ();

	ok = 1;
	for (i = 1; i <= n; i++)
		if (a [i] != i)
			End ();
        sc = fopen("tmp.txt", "w");
	fprintf(sc, "10\n");
        fclose(sc);
	printf ("10\n");

	fclose (in);
	fclose (out);
	fclose (sol);
	return 0;
}
