Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar.

<image>

So, if height of Xaniar is h1 and height of Abol is h2, after one second height of Xaniar will become <image> and height of Abol will become <image> where x1, y1, x2 and y2 are some integer numbers and <image> denotes the remainder of a modulo b.

Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is a1 and height of Abol is a2.

Mike has asked you for your help. Calculate the minimum time or say it will never happen.

Input

The first line of input contains integer m (2 ≤ m ≤ 106).

The second line of input contains integers h1 and a1 (0 ≤ h1, a1 < m).

The third line of input contains integers x1 and y1 (0 ≤ x1, y1 < m).

The fourth line of input contains integers h2 and a2 (0 ≤ h2, a2 < m).

The fifth line of input contains integers x2 and y2 (0 ≤ x2, y2 < m).

It is guaranteed that h1 ≠ a1 and h2 ≠ a2.

Output

Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.

Examples

Input

5
4 2
1 1
0 1
2 3


Output

3


Input

1023
1 2
1 0
1 2
1 1


Output

-1

Note

In the first sample, heights sequences are following:

Xaniar: <image>

Abol: <image>
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.StringTokenizer;


public class A
{
        String line;
        StringTokenizer inputParser;
        BufferedReader is;
        FileInputStream fstream;
        DataInputStream in;
        String FInput="";
        
        void openInput(String file)
        {
            if(file==null)is = new BufferedReader(new InputStreamReader(System.in));//stdin
            else
            {
                    try{
            
                            
                    fstream = new FileInputStream(file);
                    in = new DataInputStream(fstream);
                    is = new BufferedReader(new InputStreamReader(in));
                    }catch(Exception e)
                    {
                            System.err.println(e);
                    }
            }
        }
        
        void readNextLine()
    	{
    		try {
    			line = is.readLine();
    			inputParser = new StringTokenizer(line, " ,\t");
    			//System.err.println("Input: " + line);
    		} catch (IOException e) {
    			System.err.println("Unexpected IO ERROR: " + e);
    		}	
    		catch (NullPointerException e)
    		{
    			line=null;
    			
    		}
    		
    	}
        
        long NextLong()
        {
                String n = inputParser.nextToken();
                
                long val = Long.parseLong(n);
                
                 return val;
        }
        
        int NextInt()
        {
                String n = inputParser.nextToken();
                int val = Integer.parseInt(n);
                
                //System.out.println("I read this number: " + val);
                return val;
        }
        
        double NextDouble()
        {
                String n = inputParser.nextToken();
                double val = Double.parseDouble(n);
                
                //System.out.println("I read this number: " + val);
                return val;
        }
        
        String NextString()
        {
                String n = inputParser.nextToken();
                return n;
        }
        
        void closeInput()
        {
                try {
                        is.close();
                } catch (IOException e) {
                        System.err.println("Unexpected IO ERROR: " + e);
                }
                        
        }
        
        
        public static void main(String [] argv)
        {
                //String filePath="circles.in";
                String filePath=null;
                if(argv.length>0)filePath=argv[0];
                new A(filePath);
                
        }
        
        //long [] sums = new long[1000001];
        public A(String inputFile)
        {
        	Random rgen = new Random();
        	openInput(inputFile);
    		//readNextLine();
    		int T=1;//NextInt();
    		StringBuilder sb = new StringBuilder();
    		int idLine=0;
    		for(int t=1; t<=T; t++)
    		{
    			readNextLine();
    			int MOD=NextInt();
    			readNextLine();
    			int h1=NextInt();
    			int a1=NextInt();
    			readNextLine();
    			int x1=NextInt();
    			int y1=NextInt();
    			readNextLine();
    			int h2=NextInt();
    			int a2=NextInt();
    			readNextLine();
    			int x2=NextInt();
    			int y2=NextInt();
    			
    			int startPeriod1, startPeriod2;
    			
    			int time=0;
    			long H1=h1, H2=h2;
    			while(time<1000000)
    			{
    				if(H1==a1&&H2==a2)
    				{
    					System.out.println(time);
    					return;
    				}
    				H1=(H1*x1+y1)%MOD;
    				H2=(H2*x2+y2)%MOD;
    				time++;
    			}
    			
    			int [] s = new int[MOD];
    			for(int i=0; i<MOD; i++)
    				s[i]=-1;
    			
    			int i=0;
    			while(s[h1]==-1)
    			{
    				s[h1]=i;
    				h1 = (int) (((long)x1*h1+y1) % MOD);
    				i++;
    			}
    			int p1=i-s[h1];
    			boolean poss=true;
    			if(s[a1]==-1)
    			{
    				poss=false;
    			}
    			else
    			{
    				startPeriod1=h1;
    				poss=false;
    				do
    				{
    					if(h1==a1)
    					{
    						poss=true;
    						break;
    					}
    					h1=(int) (((long)x1*h1+y1) % MOD);
    				}while(h1!=startPeriod1);
    			}
    			
    			int [] s2 = new int[MOD];
    			for( i=0; i<MOD; i++)
    				s2[i]=-1;
    			
    			i=0;
    			while(s2[h2]==-1)
    			{
    				s2[h2]=i;
    				h2 = (int) (((long)x2*h2+y2) % MOD);
    				i++;
    			}
    			int p2=i-s2[h2];
    			boolean poss2=true;
    			if(s2[a2]==-1)
    			{
    				poss2=false;
    			}
    			else
    			{
    				startPeriod2=h2;
    				poss2=false;
    				do
    				{
    					if(h2==a2)
    					{
    						poss2=true;
    						break;
    					}
    					h2=(int) (((long)x2*h2+y2) % MOD);
    				}while(h2!=startPeriod2);
    			}
    			if(poss&&poss2)
    			{
    				long G=GCD(p1,p2);
    				long t1=s[a1];
    				long t2=s2[a2];
    				G = (long)p1 * p2 / G;
    				for(int g=0; g<=G; g++)
    				{
    					if(t1==t2)break;
    					if(t1<t2)
    					{
    						long d=t2-t1;
    						d /= p1;
    						d--;
    						t1+=d*p1;
    						while(t1<t2)t1+=p1;
    					}
    					if(t2<t1)
    					{
    						long d=t1-t2;
    						d /= p2;
    						d--;
    						t2+=d*p2;
    						while(t2<t1)t2+=p2;
    					}
    						
    				}
    				if(t1!=t2)
    					t1=-1;
    				sb.append(t1);
    			}
    			else
    			{
    				if(s[a1]==s2[a2])sb.append(s[a1]);
    				else sb.append(-1);
    			}
    		}
    		System.out.println(sb);
    		
    		
    		
    		
    		closeInput();		
      
        }
        
        private int GCD(int a, int b)
    	{
    		return(b==0?a:GCD(b, a%b));
    	}
}