/* 
   AOJ 2149
   Luck Manipulator
   2015.4.26
 */

#include<stdio.h>

int calc(int reel, int a, int b, int c, int init_random)
{
  int i, total_flames, random_num = init_random, target_number[100] = {0};


  for(i = 0; i < reel; i++)
    {
      scanf("%d", &target_number[i]);

    }

  for(total_flames = 0, i = 0; ; random_num = (a * random_num + b) % c, total_flames++)
    {
      if(random_num == target_number[i])
	{
	  i++;
	}

      if(total_flames > 10000 || i >= reel)
	{
	  break;
	}
    }

  if(total_flames > 10000)
    {
      return -1;
    }

  return total_flames;
}

int main(void)
{
  int n, a, b, c, x, flames;
  
  while(1)
    {
      scanf("%d %d %d %d %d", &n, &a, &b, &c, &x);
      if(n == 0 && a == 0 && b == 0 && c == 0 && x == 0)
	{
	  break;
	}
      
      flames = calc(n, a, b, c, x);
      printf("%d\n", flames);
      
    }
  return 0;
}