using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Numerics;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    int Gcd(int a, int b)
    {
        return b > 0 ? Gcd(b, a % b) : a;
    }

    public void Solve()
    {
        var a = ReadIntMatrix(2);

        if (a[0][0] == 0 && a[0][1] == 0)
        {
            if (a[0][2] != 0 || a[1][0] == 0 && a[1][1] == 0 && a[1][2] != 0)
                Write(0);
            else
                Write(-1);
            return;
        }

        if (a[1][0] == 0 && a[1][1] == 0)
        {
            Write(a[1][2] == 0 ? -1 : 0);
            return;
        }

        if (a[0][0] == 0 && a[1][0] == 0)
            Write(a[0][1] * a[1][2] == a[1][1] * a[0][2] ? -1 : 0);
        else if (a[0][1] == 0 && a[1][1] == 0)
            Write(a[0][0] * a[1][2] == a[1][0] * a[0][2] ? -1 : 0);
        else if (a[0][0] * a[1][1] == a[0][1] * a[1][0])
            Write(a[0][2] == a[1][2] ? -1 : 0);
        else
            Write(1);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
#if DEBUG
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

class Program
{
    public static int[] Read(string str)
    {
        string[] tokens = str.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        int[] ret = new int[tokens.Length];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = int.Parse(tokens[i]);
        }

        return ret;
    }

    public static long[] ReadLong(string str)
    {
        string[] tokens = str.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        long[] ret = new long[tokens.Length];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = long.Parse(tokens[i]);
        }

        return ret;
    }

    public static double[] ReadDouble(string str)
    {
        string[] tokens = str.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        double[] ret = new double[tokens.Length];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = double.Parse(tokens[i], CultureInfo.InvariantCulture);
        }

        return ret;
    }

    static string Reverse(string s)
    {
        char[] a = s.ToCharArray();
        Array.Reverse(a);
        return new string(a);
    }

    static bool Check(string t)
    {
        for (int i = 0; i < t.Length; i++)
        {
            if (char.IsLetterOrDigit(t[i]) == false && t[i] != '_')
            {
                return false;
            }
        }

        return true;
    }

    static void Main(string[] args)
    {
        int[] abc = Read(Console.ReadLine());
        int[] def = Read(Console.ReadLine());

        if (abc[0] * def[1] == abc[1] * def[0])
        {
            if (abc[0] * def[2] == abc[2] * def[0] && abc[1] * def[2] == abc[2] * def[1])
            {
                Console.WriteLine("-1");
            }
            else
            {
                Console.WriteLine("0");
            }
        }
        else
        {
            Console.WriteLine("1");
        }
    }
}


using System;

public class Program
{
	public static void Main()
	{
		string[] input = Console.ReadLine().Split(' ');
		double a1 = double.Parse(input[0]);
		double b1 = double.Parse(input[1]);
		double c1 = double.Parse(input[2]);

		input = Console.ReadLine().Split(' ');
		double a2 = double.Parse(input[0]);
		double b2 = double.Parse(input[1]);
		double c2 = double.Parse(input[2]);

		//if at least one is empty set
		if((a1 == 0 && b1 == 0 && c1 != 0) || (a2 == 0 && b2 == 0 && c2 != 0))
		{
			Console.WriteLine(0);
		}
		//else if at least one is infinite
		else if((a1 == 0 && b1 == 0 && c1 == 0) || (a2 == 0 && b2 == 0 && c2 == 0))
		{
			Console.WriteLine(-1);
		}
		//else
		else if(a1 == 0 && a2 == 0 && c1 / b1 == c2 / b2)
		{
			Console.WriteLine(-1);
		}
		else if(a1 == 0 && a2 == 0 && c1 / b1 != c2 / b2)
		{
			Console.WriteLine(0);
		}
		else if(b1 == 0 && b2 == 0 && c1 / a1 == c2 / a2)
		{
			Console.WriteLine(-1);
		}
		else if(b1 == 0 && b2 == 0 && c1 / a1 != c2 / a2)
		{
			Console.WriteLine(0);
		}
		else if(a1 / b1 == a2 / b2 && c1 == c2)
		{
			Console.WriteLine(-1);
		}
		else if(a1 / b1 == a2 / b2 && c1 != c2)
		{
			Console.WriteLine(0);
		}
		else
		{
			Console.WriteLine(1);
		}
	}
}


using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        string line1 = Console.ReadLine();
        string line2 = Console.ReadLine();
        string[] data1 = line1.Split(new char[] { ' ' });
        string[] data2 = line2.Split(new char[] { ' ' });

        Console.WriteLine(new Solver().Solve(int.Parse(data1[0]), int.Parse(data1[1]), int.Parse(data1[2]),
            int.Parse(data2[0]), int.Parse(data2[1]), int.Parse(data2[2])));
    }
}

class Solver
{
    public int Solve(int a1, int b1, int c1, int a2, int b2, int c2)
    {
        if (a1 == 0 && b1 == 0) return 0;
        if (a2 == 0 && b2 == 0) return 0;
        if ((a1 == 0 && b2 == 0) || (a2 == 0 && b1 == 0)) return 1;
        if (a1 == 0 && a2 == 0)
        {
            if (b1 * c2 == b2 * c1) return -1;
            return 0;
        }
        if (b1 == 0 && b2 == 0)
        {
            if (a1 * c2 == a2 * c1) return -1;
            return 0;
        }

        bool a_same = (a1 * b2 == a2 * b1);
        bool b_same = (c1 * b2 == b1 * c2);
        return (a_same && b_same) ? -1 : (a_same ? 0 : 1);

    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Numerics;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    int Gcd(int a, int b)
    {
        return b > 0 ? Gcd(b, a % b) : a;
    }

    public void Solve()
    {
        var a = ReadIntMatrix(2);

        if (a[0][0] == 0 && a[0][1] == 0)
        {
            if (a[0][2] != 0 || a[1][0] == 0 && a[1][1] == 0 && a[1][2] != 0)
                Write(0);
            else
                Write(-1);
            return;
        }

        if (a[1][0] == 0 && a[1][1] == 0)
        {
            Write(a[1][2] == 0 ? -1 : 0);
            return;
        }

        for (int i = 0; i < 2; i++)
        {
            int g = 0;
            for (int j = 0; j < 3; j++)
                g = Gcd(g, Math.Abs(a[i][j]));
            for (int j = 0; j < 3; j++)
                a[i][j] /= g;

            if (a[i][0] < 0 || a[i][0] == 0 && a[i][1] < 0)
                for (int j = 0; j < 3; j++)
                    a[i][j] = -a[i][j];
        }

        if (a[0][0] == a[1][0] && a[0][1] == a[1][1])
            Write(a[0][2] == a[1][2] ? -1 : 0);
        else
            Write(1);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
#if DEBUG
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Numerics;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    int Gcd(int a, int b)
    {
        return b > 0 ? Gcd(b, a % b) : a;
    }

    public void Solve()
    {
        var a = ReadIntMatrix(2);

        if (a[0][0] == 0 && a[0][1] == 0)
        {
            if (a[0][2] != 0 || a[1][0] == 0 && a[1][1] == 0 && a[1][2] != 0)
                Write(0);
            else
                Write(-1);
            return;
        }

        if (a[1][0] == 0 && a[1][1] == 0)
        {
            Write(a[1][2] == 0 ? -1 : 0);
            return;
        }

        if (a[0][0] * a[1][1] == a[0][1] * a[1][0])
            Write(a[0][2] == a[1][2] ? -1 : 0);
        else
            Write(1);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
#if DEBUG
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}


using System;

public class Program
{
	public static void Main()
	{
		string[] input = Console.ReadLine().Split(' ');
		double a1 = double.Parse(input[0]);
		double b1 = double.Parse(input[1]);
		double c1 = double.Parse(input[2]);

		input = Console.ReadLine().Split(' ');
		double a2 = double.Parse(input[0]);
		double b2 = double.Parse(input[1]);
		double c2 = double.Parse(input[2]);

		if((a1 == 0 && b1 == 0 && c1 == 0) || (a2 == 0 && b2 == 0 && c2 == 0))
		{
			Console.WriteLine(-1);
		}
		else if((a1 == 0 && b1 == 0 && c1 != 0) || (a2 == 0 && b2 == 0 && c2 != 0))
		{
			Console.WriteLine(0);
		}
		else if(b1 == 0 && b2 == 0 && c1 == c2)
		{
			Console.WriteLine(-1);
		}
		else if(b1 == 0 && b2 == 0 && c1 != c2)
		{
			Console.WriteLine(0);
		}
		else if(a1 / b1 == a2 / b2 && c1 == c2)
		{
			Console.WriteLine(-1);
		}
		else if(a1 / b1 == a2 / b2 && c1 != c2)
		{
			Console.WriteLine(0);
		}
		else
		{
			Console.WriteLine(1);
		}
	}
}


using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace codeforces
{
    class C
    {
        static CodeforcesUtils CF = new CodeforcesUtils(
@"
0 0 0
1 1 0
");

        static void Main(string[] args)
        {
            CF.WriteLine(_t());
        }

        static int _t()
        {
            string[] ss = CF.ReadLine().Split(' ');
            int x1 = int.Parse(ss[0]);
            int y1 = int.Parse(ss[1]);
            int c1 = int.Parse(ss[2]);
            ss = CF.ReadLine().Split(' ');
            int x2 = int.Parse(ss[0]);
            int y2 = int.Parse(ss[1]);
            int c2 = int.Parse(ss[2]);

            int t1 = _t2(x1, y1, c1);
            if (t1 == 0)
                return 0;
            int t2 = _t2(x1, y1, c1);
            if (t2 == 0)
                return 0;
            if (t1 == -1)
                return -1;
            if (t2 == -1)
                return -1;

            if (x1 * y2 == x2 * y1)
            {
                if ((x1 * c2 == x2 * c1) && (y1 * c2 == y2 * c1))
                    return -1;
                else
                    return 0;
            }
            else
                return 1;
        }

        static int _t2(int a, int b, int c)
        {
            if (a == 0 && b == 0)
            {
                if (c == 0)
                    return -1;
                else
                    return 0;
            }
            return 1;
        }

        #region test

        class CodeforcesUtils
        {
            public string ReadLine()
            {
#if DEBUG
                if (_lines == null)
                {
                    _lines = new List<string>();
                    string[] ss = _test_input.Replace("\n", "").Split('\r');
                    for (int i = 0; i < ss.Length; i++)
                    {
                        if (
                            (i == 0 || i == ss.Length - 1) &&
                            ss[i].Length == 0
                            )
                            continue;

                        _lines.Add(ss[i]);
                    }
                }

                string s = null;
                if (_lines.Count > 0)
                {
                    s = _lines[0];
                    _lines.RemoveAt(0);
                }
                return s;
#else
            return Console.In.ReadLine();
#endif
            }

            public void WriteLine(object o)
            {
#if DEBUG
                System.Diagnostics.Trace.WriteLine(o);
#else
            Console.WriteLine(o);
#endif
            }

            public void Write(object o)
            {
#if DEBUG
                System.Diagnostics.Trace.Write(o);
#else
            Console.Write(o);
#endif
            }

            public CodeforcesUtils(string test_input)
            {
                _test_input = test_input;
            }

            string _test_input;

            List<string> _lines;
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Numerics;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    int Gcd(int a, int b)
    {
        return b > 0 ? Gcd(b, a % b) : a;
    }

    public void Solve()
    {
        var a = ReadIntMatrix(2);

        if (a[0][0] == 0 && a[0][1] == 0)
        {
            if (a[0][2] != 0 || a[1][0] == 0 && a[1][1] == 0 && a[1][2] != 0)
                Write(0);
            else
                Write(-1);
            return;
        }

        if (a[1][0] == 0 && a[1][1] == 0)
        {
            Write(a[1][2] == 0 ? -1 : 0);
            return;
        }

        if (a[0][0] == 0 && a[1][0] == 0)
            Write(a[0][1] * a[1][2] == a[1][1] * a[0][2] ? -1 : 0);
        else if (a[0][1] == 0 && a[1][1] == 1)
            Write(a[0][0] * a[1][2] == a[1][0] * a[0][2] ? -1 : 0);
        else if (a[0][0] * a[1][1] == a[0][1] * a[1][0])
            Write(a[0][2] == a[1][2] ? -1 : 0);
        else
            Write(1);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
#if DEBUG
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeForces
{
    static class Reader
    {
        public static int NextInt()
        {
            int ret = 0;
            char c;

            while (!char.IsDigit(c = (char)Console.Read()))
            {
                if (c == '-')
                    return -NextInt();
            }
            do
            {
                ret = ret * 10 + c - '0';
            } while (char.IsDigit(c = (char)Console.Read()));

            return ret;
        }

        public static double NextDouble()
        {
            double ret = 0;
            char c;

            while (!char.IsDigit(c = (char)Console.Read()))
            {
                if (c == '-')
                    return -NextDouble();
            }
            do
            {
                ret = ret * 10 + c - '0';
            } while (char.IsDigit(c = (char)Console.Read()));
            if (c != '.') return ret;

            double p10 = 0.1;
            while (char.IsDigit(c = (char)Console.Read()))
            {
                ret += p10 * (c - '0');
                p10 *= 0.1;
            }

            return ret;
        }

        public static string NextToken()
        {
            StringBuilder ret = new StringBuilder();
            char c;

            while (char.IsWhiteSpace(c = (char)Console.Read())) ;
            do
            {
                ret.Append(c);
            } while (!char.IsWhiteSpace(c = (char)Console.Read()));

            return ret.ToString();
        }
    }

    class Program
    {
        static int GCD(int n, int m)
        {
            return m > 0 ? GCD(m, n % m) : n;
        }

        static void Get(out int A, out int B, out int C)
        {
            int a = Reader.NextInt();
            int b = Reader.NextInt();
            int c = Reader.NextInt();

            int g = GCD(Math.Abs(a), GCD(Math.Abs(b), Math.Abs(c)));
            if (g == 0)
                g = 1;

            A = a / g;
            B = b / g;
            C = c / g;

            if (A <= 0 && B <= 0 && C <= 0)
            {
                A = -A;
                B = -B;
                C = -C;
            }
        }

        static void Main(string[] args)
        {
            int A1, B1, C1, A2, B2, C2;

            Get(out A1, out B1, out C1);
            Get(out A2, out B2, out C2);

            bool inf = (A1 == 0 && B1 == 0 && C1 == 0) || (A2 == 0 && B2 == 0 && C2 == 0);
            bool no = (A1 == 0 && B1 == 0 && C1 != 0) || (A2 == 0 && B2 == 0 && C2 != 0);

            if ((A1 == A2 && B1 == B2 && C1 == C2) || (inf && !no))
            {
                Console.WriteLine(-1);
                return;
            }

            if (A1 * B2 == A2 * B1 || no)
            {
                Console.WriteLine(0);
                return;
            }

            Console.WriteLine(1);
        }
    }
}


using System;

public class Program
{
	public static void Main()
	{
		string[] input = Console.ReadLine().Split(' ');
		double a1 = double.Parse(input[0]);
		double b1 = double.Parse(input[1]);
		double c1 = double.Parse(input[2]);

		input = Console.ReadLine().Split(' ');
		double a2 = double.Parse(input[0]);
		double b2 = double.Parse(input[1]);
		double c2 = double.Parse(input[2]);

		//if at least one is empty set
		if((a1 == 0 && b1 == 0 && c1 != 0) || (a2 == 0 && b2 == 0 && c2 != 0))
		{
			Console.WriteLine(0);
		}
		//else if at least one is infinite
		else if((a1 == 0 && b1 == 0 && c1 == 0) || (a2 == 0 && b2 == 0 && c2 == 0))
		{
			Console.WriteLine(-1);
		}
		//else
		else if(a1 == 0 && a2 == 0 && c1 / b1 == c2 / b2)
		{
			Console.WriteLine(-1);
		}
		else if(a1 == 0 && a2 == 0 && c1 / b1 != c2 / b2)
		{
			Console.WriteLine(0);
		}
		else if(b1 == 0 && b2 == 0 && c1 / a1 == c2 / a2)
		{
			Console.WriteLine(-1);
		}
		else if(b1 == 0 && b2 == 0 && c1 / a1 != c2 / a2)
		{
			Console.WriteLine(0);
		}
		else if(a1 / b1 == a2 / b2 && c1 / b1 == c2 / b2)
		{
			Console.WriteLine(-1);
		}
		else if(a1 / b1 == a2 / b2 && c1 / b1 != c2 / b2)
		{
			Console.WriteLine(0);
		}
		else
		{
			Console.WriteLine(1);
		}
	}
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Numerics;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public void Solve()
    {
        var a = ReadIntMatrix(2);

        if (a[0][0] == 0 && a[0][1] == 0)
        {
            if (a[0][2] != 0 || a[1][0] == 0 && a[1][1] == 0 && a[1][2] != 0)
                Write(0);
            else
                Write(-1);
            return;
        }

        if (a[1][0] == 0 && a[1][1] == 0)
        {
            Write(a[1][2] == 0 ? -1 : 0);
            return;
        }

        if (a[0][0] == 0 && a[1][0] == 0)
            Write(a[0][1] * a[1][2] == a[1][1] * a[0][2] ? -1 : 0);
        else if (a[0][1] == 0 && a[1][1] == 0)
            Write(a[0][0] * a[1][2] == a[1][0] * a[0][2] ? -1 : 0);
        else if (a[0][0] * a[1][1] == a[0][1] * a[1][0])
            Write(a[0][2] * a[1][0] == a[1][2] * a[0][0] ? -1 : 0);
        else
            Write(1);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
#if DEBUG
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeForces
{
    static class Reader
    {
        public static int NextInt()
        {
            int ret = 0;
            char c;

            while (!char.IsDigit(c = (char)Console.Read()))
            {
                if (c == '-')
                    return -NextInt();
            }
            do
            {
                ret = ret * 10 + c - '0';
            } while (char.IsDigit(c = (char)Console.Read()));

            return ret;
        }

        public static double NextDouble()
        {
            double ret = 0;
            char c;

            while (!char.IsDigit(c = (char)Console.Read()))
            {
                if (c == '-')
                    return -NextDouble();
            }
            do
            {
                ret = ret * 10 + c - '0';
            } while (char.IsDigit(c = (char)Console.Read()));
            if (c != '.') return ret;

            double p10 = 0.1;
            while (char.IsDigit(c = (char)Console.Read()))
            {
                ret += p10 * (c - '0');
                p10 *= 0.1;
            }

            return ret;
        }

        public static string NextToken()
        {
            StringBuilder ret = new StringBuilder();
            char c;

            while (char.IsWhiteSpace(c = (char)Console.Read())) ;
            do
            {
                ret.Append(c);
            } while (!char.IsWhiteSpace(c = (char)Console.Read()));

            return ret.ToString();
        }
    }

    class Program
    {
        static int GCD(int n, int m)
        {
            return m > 0 ? GCD(m, n % m) : n;
        }

        static void Get(out int A, out int B, out int C)
        {
            int a = Reader.NextInt();
            int b = Reader.NextInt();
            int c = Reader.NextInt();

            int g = GCD(Math.Abs(a), GCD(Math.Abs(b), Math.Abs(c)));
            if (g == 0)
                g = 1;

            A = a / g;
            B = b / g;
            C = c / g;
        }

        static void Main(string[] args)
        {
            int A1, B1, C1, A2, B2, C2;

            Get(out A1, out B1, out C1);
            Get(out A2, out B2, out C2);

            bool inf = (A1 == 0 && B1 == 0 && C1 == 0) || (A2 == 0 && B2 == 0 && C2 == 0);
            bool no = (A1 == 0 && B1 == 0 && C1 != 0) || (A2 == 0 && B2 == 0 && C2 != 0);

            if (!no && ((A1 == A2 && B1 == B2 && C1 == C2) || (A1 == -A2 && B1 == -B2 && C1 == -C2) || inf))
            {
                Console.WriteLine(-1);
                return;
            }

            if (A1 * B2 == A2 * B1 || no)
            {
                Console.WriteLine(0);
                return;
            }

            Console.WriteLine(1);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

class Program
{
    public static int[] Read(string str)
    {
        string[] tokens = str.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        int[] ret = new int[tokens.Length];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = int.Parse(tokens[i]);
        }

        return ret;
    }

    public static long[] ReadLong(string str)
    {
        string[] tokens = str.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        long[] ret = new long[tokens.Length];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = long.Parse(tokens[i]);
        }

        return ret;
    }

    public static double[] ReadDouble(string str)
    {
        string[] tokens = str.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        double[] ret = new double[tokens.Length];
        for (int i = 0; i < ret.Length; i++)
        {
            ret[i] = double.Parse(tokens[i], CultureInfo.InvariantCulture);
        }

        return ret;
    }

    static string Reverse(string s)
    {
        char[] a = s.ToCharArray();
        Array.Reverse(a);
        return new string(a);
    }

    static bool Check(string t)
    {
        for (int i = 0; i < t.Length; i++)
        {
            if (char.IsLetterOrDigit(t[i]) == false && t[i] != '_')
            {
                return false;
            }
        }

        return true;
    }

    static void Main(string[] args)
    {
        int[] abc = Read(Console.ReadLine());
        int[] def = Read(Console.ReadLine());

        if ((abc[0] == 0 && abc[1] == 0 && abc[2] != 0) || (def[0] == 0 && def[1] == 0 && def[2] != 0))
        {
            Console.WriteLine("0");
            return;
        }

        if (abc[0] * def[1] == abc[1] * def[0])
        {
            if (abc[0] * def[2] == abc[2] * def[0] && abc[1] * def[2] == abc[2] * def[1])
            {
                Console.WriteLine("-1");
            }
            else
            {
                Console.WriteLine("0");
            }
        }
        else
        {
            Console.WriteLine("1");
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace codeforces
{
    class C
    {
        static CodeforcesUtils CF = new CodeforcesUtils(
@"
0 0 0
1 1 0
");

        static void Main(string[] args)
        {
            CF.WriteLine(_t());
        }

        static int _t()
        {
            string[] ss = CF.ReadLine().Split(' ');
            int x1 = int.Parse(ss[0]);
            int y1 = int.Parse(ss[1]);
            int c1 = int.Parse(ss[2]);
            ss = CF.ReadLine().Split(' ');
            int x2 = int.Parse(ss[0]);
            int y2 = int.Parse(ss[1]);
            int c2 = int.Parse(ss[2]);

            int t1 = _t2(x1, y1, c1);
            if (t1 == 0)
                return 0;
            int t2 = _t2(x2, y2, c2);
            if (t2 == 0)
                return 0;
            if (t1 == -1)
                return -1;
            if (t2 == -1)
                return -1;

            if (x1 * y2 == x2 * y1)
            {
                if ((x1 * c2 == x2 * c1) && (y1 * c2 == y2 * c1))
                    return -1;
                else
                    return 0;
            }
            else
                return 1;
        }

        static int _t2(int a, int b, int c)
        {
            if (a == 0 && b == 0)
            {
                if (c == 0)
                    return -1;
                else
                    return 0;
            }
            return 1;
        }

        #region test

        class CodeforcesUtils
        {
            public string ReadLine()
            {
#if DEBUG
                if (_lines == null)
                {
                    _lines = new List<string>();
                    string[] ss = _test_input.Replace("\n", "").Split('\r');
                    for (int i = 0; i < ss.Length; i++)
                    {
                        if (
                            (i == 0 || i == ss.Length - 1) &&
                            ss[i].Length == 0
                            )
                            continue;

                        _lines.Add(ss[i]);
                    }
                }

                string s = null;
                if (_lines.Count > 0)
                {
                    s = _lines[0];
                    _lines.RemoveAt(0);
                }
                return s;
#else
            return Console.In.ReadLine();
#endif
            }

            public void WriteLine(object o)
            {
#if DEBUG
                System.Diagnostics.Trace.WriteLine(o);
#else
            Console.WriteLine(o);
#endif
            }

            public void Write(object o)
            {
#if DEBUG
                System.Diagnostics.Trace.Write(o);
#else
            Console.Write(o);
#endif
            }

            public CodeforcesUtils(string test_input)
            {
                _test_input = test_input;
            }

            string _test_input;

            List<string> _lines;
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        string line1 = Console.ReadLine();
        string line2 = Console.ReadLine();
        string[] data1 = line1.Split(new char[] { ' ' });
        string[] data2 = line2.Split(new char[] { ' ' });

        Console.WriteLine(new Solver().Solve(int.Parse(data1[0]), int.Parse(data1[1]), int.Parse(data1[2]),
            int.Parse(data2[0]), int.Parse(data2[1]), int.Parse(data2[2])));
    }
}

class Solver
{
    public int Solve(int a1, int b1, int c1, int a2, int b2, int c2)
    {
        if (a1 == 0 && b1 == 0 && c1 != 0) return 0;
        if (a2 == 0 && b2 == 0 && c2 != 0) return 0;
        if ((a1 == 0 && b1 == 0) || (a2 == 0 && b2 == 0)) return -1;

        if ((a1 == 0 && b2 == 0) || (a2 == 0 && b1 == 0)) return 1;
        if (a1 == 0 && a2 == 0)
        {
            if (b1 * c2 == b2 * c1) return -1;
            return 0;
        }
        if (b1 == 0 && b2 == 0)
        {
            if (a1 * c2 == a2 * c1) return -1;
            return 0;
        }

        bool a_same = (a1 * b2 == a2 * b1);
        bool b_same = (c1 * b2 == b1 * c2);
        return (a_same && b_same) ? -1 : (a_same ? 0 : 1);

    }
}


using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    class Line
    {
        public long a, b, c;
        public Line(long a, long b, long c)
        {
            this.a = a;
            this.b = b;
            this.c = c;
        }
        public bool isLine()
        {
            if (a == 0 && b == 0 && c != 0)
                return false;
            return true;
        }
        public bool isPlane()
        {
            return (a == 0 && b == 0 && c == 0);
        }

    }
    void solve()
    {
        Line first = new Line(nextInt(),nextInt(),-nextInt());
        Line second = new Line(nextInt(), nextInt(), -nextInt());
        int ret = 0;
        if (!first.isLine() || !second.isLine())
            ret = 0;
        else
        {
            if (first.isPlane() || second.isPlane())
                ret = -1;
            else
            {
                long det = first.b * second.a - first.a * second.b;
                long o = second.a * first.c - first.a * second.c;
                if (det == 0)
                {
                    if (o == 0)
                        ret = -1;
                    else
                        ret = 0;
                }
                else
                {
                    ret = 1;
                }
            }
        }
        println(ret);

    }

    ////////////

    private void println(string Stringst)
    {
        Console.WriteLine(Stringst);
    }
    private void println(char charnum)
    {
        Console.WriteLine(charnum);
    }
    private void println(int Intnum)
    {
        Console.WriteLine(Intnum);
    }
    private void println(long Longnum)
    {
        Console.WriteLine(Longnum);
    }
    private void println(double Doublenum)
    {
        Console.WriteLine(Doublenum);
    }

    private void print(string Stringst)
    {
        Console.Write(Stringst);
    }
    private void print(int Intnum)
    {
        Console.Write(Intnum);
    }
    private void print(char charnum)
    {
        Console.Write(charnum);
    }
    private void print(long Longnum)
    {
        Console.Write(Longnum);
    }
    private void print(double Doublenum)
    {
        Console.Write(Doublenum);
    }

    string[] inputLine = new string[0];
    int inputInd = 0;
    string nextLine()
    {
        return Console.ReadLine();
    }
    void readInput()
    {
        if (inputInd != inputLine.Length)
            throw new Exception();
        inputInd = 0;
        inputLine = Console.ReadLine().Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
        if (inputLine.Length == 0)
            readInput();

    }
    int nextInt()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return int.Parse(inputLine[inputInd++]);
    }
    long nextLong()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return long.Parse(inputLine[inputInd++]);
    }
    double nextDouble()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return double.Parse(inputLine[inputInd++]);
    }
    string nextString()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return inputLine[inputInd++];
    }
    static void Main(string[] args)
    {
        new Program().solve();
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;

using E = System.Linq.Enumerable;
using System.Threading;
using System.Numerics;

internal partial class Solver {
    public void Run() {
        var m = nl();
        var x = nl();

        cout.WriteLine(Solve(m, x));
    }

    /*
    Sample 1
    0 -> 0
    1 -> 3 -> 1 -> 3
    2 -> 2 -> 2 -> 2 (2*3^x%4 --> 3^x%2 と等化)
    3 -> 1 -> 3 -> 1

    g = gcd(start, M) ごとに場合分けすればよい
    start' = start / g
    M' = M / g
    start, start * x, ... start * x^k % M ->
    (start/g), start, ... start * x^k % M')
    start', start' * x, ... start' * x^k % M'

     * */

    private long Solve(long m, long x) {
        // from i to (i*x)%m
        long ans = 1; // for 0-room

        // x^phi(m) = 1 (mod m)
        foreach (var g in Divisors(m)) {
            var m2 = m / g;
            long minPower = long.MaxValue;
            foreach (var p in Divisors(Phi(m2))) {
                if (ModPow(x, p, m2) == 1) {
                    minPower = p;
                    break;
                }
            }
            //var startNum = (m - 1) / g;
            var startNum = Phi(m / g);
            //new { g, startNum, minPower }.TextDump();
            ans += startNum / minPower;
        }
        return ans;
    }

    public static long ModMultiple(long x, long n, long mod) {
        long t = x, ret = 0;
        while (n > 0) {
            if ((n & 1) != 0) {
                ret += t;
                if (ret >= mod) ret -= mod;
            }
            t <<= 1;
            if (t >= mod) t -= mod;
            n >>= 1;
        }
        return ret % mod;
    }

    public static long ModPow(long x, long n, long mod) {
        return (long)BigInteger.ModPow(x, n, mod);
        long t = x, ret = 1;
        t %= mod;
        while (n > 0) {
            if ((n & 1) != 0) {
                //ret = (t * ret) % mod;
                ret = ModMultiple(t, ret, mod);
            }
            //t = t * t % mod;
            t = ModMultiple(t, t, mod);
            n >>= 1;
        }
        return ret;
    }

    public static long Phi(long n) {
        long phi = n;

        for (long i = 2; i * i <= n && i < n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }

                phi = phi / i * (i - 1);
            }
        }
        if (n > 1) {
            phi = phi / n * (n - 1);
        }

        return phi;
    }

    public static List<long> Divisors(long x) {
        var ret = new List<long>();
        for (long i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                ret.Add(i);
                if (i != x / i) {
                    ret.Add(x / i);
                }
            }
        }
        ret.Sort();
        return ret;
    }

}

// PREWRITEN CODE BEGINS FROM HERE

static public class StringExtensions {
    static public string JoinToString<T>(this IEnumerable<T> source, string separator = " ") {
        return string.Join(separator, source);
    }
}

internal partial class Solver : Scanner {
    static readonly int? StackSizeInMebiByte = null; //50;
    public static void StartAndJoin(Action action, int maxStackSize) {
        var thread = new Thread(new ThreadStart(action), maxStackSize);
        thread.Start();
        thread.Join();
    }

    public static void Main() {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        if (StackSizeInMebiByte.HasValue) {
            StartAndJoin(() => new Solver(Console.In, Console.Out).Run(), StackSizeInMebiByte.Value * 1024 * 1024);
        } else {
            new Solver(Console.In, Console.Out).Run();
        }
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

#if DEBUG
internal static class LinqPadExtension {
    public static string TextDump<T>(this T obj) {
        if (obj is IEnumerable) return (obj as IEnumerable).Cast<object>().JoinToString().Dump();
        else return obj.ToString().Dump();
    }
    public static T Dump<T>(this T obj) {
        return LINQPad.Extensions.Dump(obj);
    }
}
#endif

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        int i = 0;
        bool negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) throw new FormatException();
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) break;
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;

using E = System.Linq.Enumerable;
using System.Threading;
using System.Numerics;

internal partial class Solver {
    public void Run() {
        var m = nl();
        var x = nl();

        cout.WriteLine(Solve(m, x));
    }

    /*
    Sample 1
    0 -> 0
    1 -> 3 -> 1 -> 3
    2 -> 2 -> 2 -> 2 (2*3^x%4 --> 3^x%2 と等化)
    3 -> 1 -> 3 -> 1

    g = gcd(start, M) ごとに場合分けすればよい
    start' = start / g
    M' = M / g
    start, start * x, ... start * x^k % M ->
    (start/g), start, ... start * x^k % M')
    start', start' * x, ... start' * x^k % M'

     * */

    private long Solve(long m, long x) {
        // from i to (i*x)%m
        long ans = 0;
        // x^phi(m) = 1 (mod m)
        foreach (var g in Divisors(m)) {
            var m2 = m / g;
            var phi = Phi(m2);
            long minPower = phi;
            BigInteger pow = x;
            long e = 1;
            Cache.Clear();
            foreach (var p in Divisors(phi)) {
                if (p == phi) break;
                pow = pow * ModPowRecursive(x, p - e, m2) % m2;
                //pow = pow * BigInteger.ModPow(x, p - e, m2) % m2;
                e = p;
                if (pow == BigInteger.One) {
                    minPower = p;
                    break;
                }
            }
            ans += phi / minPower;
        }
        return ans;
    }

    public static long ModMultiple(long x, long n, long mod) {
        if (x == 0 || n == 0) return 0;
        if (x < long.MaxValue / n) return x * n % mod;
        long t = x, ret = 0;
        while (n > 0) {
            if ((n & 1) != 0) {
                ret += t;
                if (ret >= mod) ret -= mod;
            }
            t <<= 1;
            if (t >= mod) t -= mod;
            n >>= 1;
        }
        return ret % mod;
    }

    Dictionary<long, BigInteger> Cache = new Dictionary<long, BigInteger>();

    public BigInteger ModPowRecursive(BigInteger x, long n, long mod) {
        if (Cache.TryGetValue(n, out var res)) return res;
        x %= mod;
        if (n == 0) {
            return Cache[n] = 1;
        } else if (n % 2 != 0) {
            return Cache[n] = x * ModPowRecursive(x, n - 1, mod) % mod;
        } else {
            var r = ModPowRecursive(x, n / 2, mod);
            return Cache[n] = r * r % mod;
        }
    }

    public static long Phi(long n) {
        long phi = n;

        for (long i = 2; i * i <= n && i < n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }

                phi = phi / i * (i - 1);
            }
        }
        if (n > 1) {
            phi = phi / n * (n - 1);
        }

        return phi;
    }

    Dictionary<long, List<long>> cache = new Dictionary<long, List<long>>();

    public List<long> Divisors(PrimeSet primes, long x) {
        //if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long> { 1 };
        foreach (var p in primes) {
            if (p * p > x) break;
            int last = 0;
            while (x % p == 0) {
                int count = ret.Count;
                for (int i = last; i < count; i++) {
                    ret.Add(ret[i] * p);
                }
                last = count;
                x /= p;
            }
        }
        if (x != 1) {
            int count = ret.Count;
            for (int i = 0; i < count; i++) {
                ret.Add(ret[i] * x);
            }
        }
        cache[x] = ret;
        return ret;
    }

    public List<long> Divisors(long x) {
        if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long>();
        for (long i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                ret.Add(i);
                if (i != x / i) {
                    ret.Add(x / i);
                }
            }
        }
        ret.Sort();
        cache[x] = ret;
        return ret;
    }

}

public class PrimeSet : List<int> {
    private readonly bool[] isPrime;

    public PrimeSet(int size)
        : base() {
        isPrime = Enumerable.Repeat(true, size).ToArray();
        Add(2);
        for (int i = 3; i <= size; i += 2) {
            if (!isPrime[i >> 1]) {
                continue;
            }

            Add(i);
            for (long j = (long)i * i; j <= size; j += i + i) {
                isPrime[j >> 1] = false;
            }
        }
    }

    public bool IsPrime(long x) {
        if (x == 2) {
            return true;
        }

        if (x <= 1 || x % 2 == 0) {
            return false;
        }

        return isPrime[x >> 1];
    }
}

// PREWRITEN CODE BEGINS FROM HERE

static public class StringExtensions {
    static public string JoinToString<T>(this IEnumerable<T> source, string separator = " ") {
        return string.Join(separator, source);
    }
}

internal partial class Solver : Scanner {
    static readonly int? StackSizeInMebiByte = null; //50;
    public static void StartAndJoin(Action action, int maxStackSize) {
        var thread = new Thread(new ThreadStart(action), maxStackSize);
        thread.Start();
        thread.Join();
    }

    public static void Main() {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        if (StackSizeInMebiByte.HasValue) {
            StartAndJoin(() => new Solver(Console.In, Console.Out).Run(), StackSizeInMebiByte.Value * 1024 * 1024);
        } else {
            new Solver(Console.In, Console.Out).Run();
        }
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

#if DEBUG
internal static class LinqPadExtension {
    public static string TextDump<T>(this T obj) {
        if (obj is IEnumerable) return (obj as IEnumerable).Cast<object>().JoinToString().Dump();
        else return obj.ToString().Dump();
    }
    public static T Dump<T>(this T obj) {
        return LINQPad.Extensions.Dump(obj);
    }
}
#endif

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        int i = 0;
        bool negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) throw new FormatException();
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) break;
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;

using E = System.Linq.Enumerable;
using System.Threading;
using System.Numerics;

internal partial class Solver {
    public void Run() {
        var m = nl();
        var x = nl();

        cout.WriteLine(Solve(m, x));
    }

    /*
    Sample 1
    0 -> 0
    1 -> 3 -> 1 -> 3
    2 -> 2 -> 2 -> 2 (2*3^x%4 --> 3^x%2 と等化)
    3 -> 1 -> 3 -> 1

    g = gcd(start, M) ごとに場合分けすればよい
    start' = start / g
    M' = M / g
    start, start * x, ... start * x^k % M ->
    (start/g), start, ... start * x^k % M')
    start', start' * x, ... start' * x^k % M'

     * */

    private long Solve(long m, long x) {
        // from i to (i*x)%m
        long ans = 0;
        // x^phi(m) = 1 (mod m)
        foreach (var g in Divisors(m)) {
            var m2 = m / g;
            var phi = Phi(m2);
            long minPower = phi;
            BigInteger pow = x;
            long e = 1;
            foreach (var p in Divisors(phi)) {
                if (p == phi) break;
                pow = pow * BigInteger.ModPow(x, p - e, m2) % m2;
                e = p;
                if (pow == BigInteger.One) {
                    minPower = p;
                    break;
                }
            }
            ans += phi / minPower;
        }
        return ans;
    }

    public static long ModMultiple(long x, long n, long mod) {
        if (x == 0 || n == 0) return 0;
        if (x < long.MaxValue / n) return x * n % mod;
        long t = x, ret = 0;
        while (n > 0) {
            if ((n & 1) != 0) {
                ret += t;
                if (ret >= mod) ret -= mod;
            }
            t <<= 1;
            if (t >= mod) t -= mod;
            n >>= 1;
        }
        return ret % mod;
    }

    public static long ModPow(long x, long n, long mod) {
        return (long)BigInteger.ModPow(x, n, mod);
        long t = x, ret = 1;
        t %= mod;
        while (n > 0) {
            if ((n & 1) != 0) {
                //ret = (t * ret) % mod;
                ret = ModMultiple(t, ret, mod);
            }
            //t = t * t % mod;
            t = ModMultiple(t, t, mod);
            n >>= 1;
        }
        return ret;
    }

    public static long Phi(long n) {
        long phi = n;

        for (long i = 2; i * i <= n && i < n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }

                phi = phi / i * (i - 1);
            }
        }
        if (n > 1) {
            phi = phi / n * (n - 1);
        }

        return phi;
    }

    Dictionary<long, List<long>> cache = new Dictionary<long, List<long>>();

    public List<long> Divisors(PrimeSet primes, long x) {
        //if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long> { 1 };
        foreach (var p in primes) {
            if (p * p > x) break;
            int last = 0;
            while (x % p == 0) {
                int count = ret.Count;
                for (int i = last; i < count; i++) {
                    ret.Add(ret[i] * p);
                }
                last = count;
                x /= p;
            }
        }
        if (x != 1) {
            int count = ret.Count;
            for (int i = 0; i < count; i++) {
                ret.Add(ret[i] * x);
            }
        }
        cache[x] = ret;
        return ret;
    }

    public List<long> Divisors(long x) {
        if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long>();
        for (long i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                ret.Add(i);
                if (i != x / i) {
                    ret.Add(x / i);
                }
            }
        }
        ret.Sort();
        cache[x] = ret;
        return ret;
    }

}

public class PrimeSet : List<int> {
    private readonly bool[] isPrime;

    public PrimeSet(int size)
        : base() {
        isPrime = Enumerable.Repeat(true, size).ToArray();
        Add(2);
        for (int i = 3; i <= size; i += 2) {
            if (!isPrime[i >> 1]) {
                continue;
            }

            Add(i);
            for (long j = (long)i * i; j <= size; j += i + i) {
                isPrime[j >> 1] = false;
            }
        }
    }

    public bool IsPrime(long x) {
        if (x == 2) {
            return true;
        }

        if (x <= 1 || x % 2 == 0) {
            return false;
        }

        return isPrime[x >> 1];
    }
}

// PREWRITEN CODE BEGINS FROM HERE

static public class StringExtensions {
    static public string JoinToString<T>(this IEnumerable<T> source, string separator = " ") {
        return string.Join(separator, source);
    }
}

internal partial class Solver : Scanner {
    static readonly int? StackSizeInMebiByte = null; //50;
    public static void StartAndJoin(Action action, int maxStackSize) {
        var thread = new Thread(new ThreadStart(action), maxStackSize);
        thread.Start();
        thread.Join();
    }

    public static void Main() {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        if (StackSizeInMebiByte.HasValue) {
            StartAndJoin(() => new Solver(Console.In, Console.Out).Run(), StackSizeInMebiByte.Value * 1024 * 1024);
        } else {
            new Solver(Console.In, Console.Out).Run();
        }
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

#if DEBUG
internal static class LinqPadExtension {
    public static string TextDump<T>(this T obj) {
        if (obj is IEnumerable) return (obj as IEnumerable).Cast<object>().JoinToString().Dump();
        else return obj.ToString().Dump();
    }
    public static T Dump<T>(this T obj) {
        return LINQPad.Extensions.Dump(obj);
    }
}
#endif

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        int i = 0;
        bool negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) throw new FormatException();
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) break;
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;

using E = System.Linq.Enumerable;
using System.Threading;
using System.Numerics;

internal partial class Solver {
    public void Run() {
        var m = nl();
        var x = nl();

        cout.WriteLine(Solve(m, x));
    }

    /*
    Sample 1
    0 -> 0
    1 -> 3 -> 1 -> 3
    2 -> 2 -> 2 -> 2 (2*3^x%4 --> 3^x%2 と等化)
    3 -> 1 -> 3 -> 1

    g = gcd(start, M) ごとに場合分けすればよい
    start' = start / g
    M' = M / g
    start, start * x, ... start * x^k % M ->
    (start/g), start, ... start * x^k % M')
    start', start' * x, ... start' * x^k % M'

     * */

    private long Solve(long m, long x) {
        // from i to (i*x)%m
        long ans = 0;
        // x^phi(m) = 1 (mod m)
        var primes = new PrimeSet(10000000);
        foreach (var g in Divisors(m)) {
            var m2 = m / g;
            var phi = Phi(m2);
            long minPower = phi;
            foreach (var p in primes) {
                if (p > minPower) break;
                if (minPower % p == 0) {
                    int cnt = 0;
                    while (minPower % p == 0) {
                        minPower /= p;
                        cnt++;
                    }
                    for (int i = 0; i < cnt; i++) {
                        if (BigInteger.ModPow(x, minPower, m2) == 1) break;
                        minPower *= p;
                    }
                }
            }

            ans += phi / minPower;
        }
        return ans;
    }
    public static long Phi(long n) {
        long phi = n;

        for (long i = 2; i * i <= n && i < n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }

                phi = phi / i * (i - 1);
            }
        }
        if (n > 1) {
            phi = phi / n * (n - 1);
        }

        return phi;
    }

    Dictionary<long, List<long>> cache = new Dictionary<long, List<long>>();

    public List<long> Divisors(PrimeSet primes, long x) {
        //if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long> { 1 };
        foreach (var p in primes) {
            if (p * p > x) break;
            int last = 0;
            while (x % p == 0) {
                int count = ret.Count;
                for (int i = last; i < count; i++) {
                    ret.Add(ret[i] * p);
                }
                last = count;
                x /= p;
            }
        }
        if (x != 1) {
            int count = ret.Count;
            for (int i = 0; i < count; i++) {
                ret.Add(ret[i] * x);
            }
        }
        cache[x] = ret;
        return ret;
    }

    public List<long> Divisors(long x) {
        if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long>();
        for (long i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                ret.Add(i);
                if (i != x / i) {
                    ret.Add(x / i);
                }
            }
        }
        ret.Sort();
        cache[x] = ret;
        return ret;
    }

}

public class PrimeSet : List<int> {
    private readonly bool[] isPrime;

    public PrimeSet(int size)
        : base() {
        isPrime = Enumerable.Repeat(true, size).ToArray();
        Add(2);
        for (int i = 3; i <= size; i += 2) {
            if (!isPrime[i >> 1]) {
                continue;
            }

            Add(i);
            for (long j = (long)i * i; j <= size; j += i + i) {
                isPrime[j >> 1] = false;
            }
        }
    }

    public bool IsPrime(long x) {
        if (x == 2) {
            return true;
        }

        if (x <= 1 || x % 2 == 0) {
            return false;
        }

        return isPrime[x >> 1];
    }
}

// PREWRITEN CODE BEGINS FROM HERE

static public class StringExtensions {
    static public string JoinToString<T>(this IEnumerable<T> source, string separator = " ") {
        return string.Join(separator, source);
    }
}

internal partial class Solver : Scanner {
    static readonly int? StackSizeInMebiByte = null; //50;
    public static void StartAndJoin(Action action, int maxStackSize) {
        var thread = new Thread(new ThreadStart(action), maxStackSize);
        thread.Start();
        thread.Join();
    }

    public static void Main() {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        if (StackSizeInMebiByte.HasValue) {
            StartAndJoin(() => new Solver(Console.In, Console.Out).Run(), StackSizeInMebiByte.Value * 1024 * 1024);
        } else {
            new Solver(Console.In, Console.Out).Run();
        }
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

#if DEBUG
internal static class LinqPadExtension {
    public static string TextDump<T>(this T obj) {
        if (obj is IEnumerable) return (obj as IEnumerable).Cast<object>().JoinToString().Dump();
        else return obj.ToString().Dump();
    }
    public static T Dump<T>(this T obj) {
        return LINQPad.Extensions.Dump(obj);
    }
}
#endif

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        int i = 0;
        bool negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) throw new FormatException();
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) break;
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;

using E = System.Linq.Enumerable;
using System.Threading;
using System.Numerics;

internal partial class Solver {
    public void Run() {
        var m = nl();
        var x = nl();

        cout.WriteLine(Solve(m, x));
    }

    /*
    Sample 1
    0 -> 0
    1 -> 3 -> 1 -> 3
    2 -> 2 -> 2 -> 2 (2*3^x%4 --> 3^x%2 と等化)
    3 -> 1 -> 3 -> 1

    g = gcd(start, M) ごとに場合分けすればよい
    start' = start / g
    M' = M / g
    start, start * x, ... start * x^k % M ->
    (start/g), start, ... start * x^k % M')
    start', start' * x, ... start' * x^k % M'

     * */

    public List<long> Factorize(long x) {
        var res = new List<long>();
        for (long i = 2; i * i <= x; i++) {
            if (x % i == 0) {
                res.Add(i);
                while (x % i == 0) x /= i;
            }
        }
        if (x != 1) res.Add(x);
        return res;
    }

    /// <returns>
    /// return min d where a^d = 1 (mod m)
    /// </returns>
    private long MultiplicativeOrder(IEnumerable<long> primes, long a, long m) {
        var phi = Phi(m);
        var res = phi;
        foreach (var p in primes) {
            if (p > phi) break;
            while (res % p == 0 && BigInteger.ModPow(a, res / p, m) == 1) {
                res /= p;
                phi /= p;
            }
            while (phi % p == 0) phi /= p;
        }
        if (phi > 1 && BigInteger.ModPow(a, res / phi, m) == 1) {
            res /= phi;
        }
        return res;
    }

    private long Solve(long m, long x) {
        // from i to (i*x)%m
        long ans = 0;
        // x^phi(m) = 1 (mod m)
        var primes = Factorize(m);
        var len = primes.Count;
        for (int i = 0; i < len; i++) {
            primes.AddRange(Factorize(primes[i] - 1));
        }
        primes.Sort();
        primes = primes.Distinct().ToList();

        foreach (var g in Divisors(m)) {
            var m2 = m / g;
            var phi = Phi(m2);
            ans += phi / MultiplicativeOrder(primes, x, m2);
        }
        return ans;
    }

    public static long ModPow(long x, long n, long mod) {
        long t = x, ret = 1;
        while (n > 0) {
            if ((n & 1) != 0) {
                ret = (long)(((decimal)t * ret) % mod);
            }
            t = (long)((decimal)t * t % mod);
            n >>= 1;
        }
        return ret;
    }

    public static long Phi(long n) {
        long phi = n;

        for (long i = 2; i * i <= n && i < n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }

                phi = phi / i * (i - 1);
            }
        }
        if (n > 1) {
            phi = phi / n * (n - 1);
        }

        return phi;
    }

    Dictionary<long, List<long>> cache = new Dictionary<long, List<long>>();

    public List<long> Divisors(PrimeSet primes, long x) {
        //if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long> { 1 };
        foreach (var p in primes) {
            if (p * p > x) break;
            int last = 0;
            while (x % p == 0) {
                int count = ret.Count;
                for (int i = last; i < count; i++) {
                    ret.Add(ret[i] * p);
                }
                last = count;
                x /= p;
            }
        }
        if (x != 1) {
            int count = ret.Count;
            for (int i = 0; i < count; i++) {
                ret.Add(ret[i] * x);
            }
        }
        cache[x] = ret;
        return ret;
    }

    public List<long> Divisors(long x) {
        if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long>();
        for (long i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                ret.Add(i);
                if (i != x / i) {
                    ret.Add(x / i);
                }
            }
        }
        ret.Sort();
        cache[x] = ret;
        return ret;
    }

}

public class PrimeSet : List<int> {
    private readonly bool[] isPrime;

    public PrimeSet(int size)
        : base() {
        isPrime = Enumerable.Repeat(true, size).ToArray();
        Add(2);
        for (int i = 3; i <= size; i += 2) {
            if (!isPrime[i >> 1]) {
                continue;
            }

            Add(i);
            for (long j = (long)i * i; j <= size; j += i + i) {
                isPrime[j >> 1] = false;
            }
        }
    }

    public bool IsPrime(long x) {
        if (x == 2) {
            return true;
        }

        if (x <= 1 || x % 2 == 0) {
            return false;
        }

        return isPrime[x >> 1];
    }
}

// PREWRITEN CODE BEGINS FROM HERE

static public class StringExtensions {
    static public string JoinToString<T>(this IEnumerable<T> source, string separator = " ") {
        return string.Join(separator, source);
    }
}

internal partial class Solver : Scanner {
    static readonly int? StackSizeInMebiByte = null; //50;
    public static void StartAndJoin(Action action, int maxStackSize) {
        var thread = new Thread(new ThreadStart(action), maxStackSize);
        thread.Start();
        thread.Join();
    }

    public static void Main() {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        if (StackSizeInMebiByte.HasValue) {
            StartAndJoin(() => new Solver(Console.In, Console.Out).Run(), StackSizeInMebiByte.Value * 1024 * 1024);
        } else {
            new Solver(Console.In, Console.Out).Run();
        }
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

#if DEBUG
internal static class LinqPadExtension {
    public static string TextDump<T>(this T obj) {
        if (obj is IEnumerable) return (obj as IEnumerable).Cast<object>().JoinToString().Dump();
        else return obj.ToString().Dump();
    }
    public static T Dump<T>(this T obj) {
        return LINQPad.Extensions.Dump(obj);
    }
}
#endif

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        int i = 0;
        bool negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) throw new FormatException();
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) break;
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}