#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
typedef long long LL;

class DivisibilityRules
{
public:
	int similar(int numerationBase, int divisor) 
	{
		vector< vector<int> > mb;
		for(int k=2; k<numerationBase; ++k)
			mb.push_back( mbase(numerationBase, k) );
		return count( mb.begin(), mb.end(), mb[divisor-2] );
	}

	vector<int> mbase(int n, int k)
	{
		vector<int> r;
		int b = 1;
		for(int i=0; i<n; ++i) {
			r.push_back(b);
			b = (b*n)%k;
		}
		return r;
	}

// BEGIN CUT HERE
	public:
	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
	private:
	template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
	void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
	void test_case_0() { int Arg0 = 10; int Arg1 = 3; int Arg2 = 2; verify_case(0, Arg2, similar(Arg0, Arg1)); }
	void test_case_1() { int Arg0 = 10; int Arg1 = 5; int Arg2 = 2; verify_case(1, Arg2, similar(Arg0, Arg1)); }
	void test_case_2() { int Arg0 = 511; int Arg1 = 32; int Arg2 = 10; verify_case(2, Arg2, similar(Arg0, Arg1)); }
	void test_case_3() { int Arg0 = 3; int Arg1 = 2; int Arg2 = 1; verify_case(3, Arg2, similar(Arg0, Arg1)); }
	void test_case_4() { int Arg0 = 1000; int Arg1 = 999; int Arg2 = 7; verify_case(4, Arg2, similar(Arg0, Arg1)); }
	void test_case_5() { int Arg0 = 655; int Arg1 = 532; int Arg2 = 1; verify_case(5, Arg2, similar(Arg0, Arg1)); }

// END CUT HERE
};
// BEGIN CUT HERE 
int main() { DivisibilityRules().run_test(-1); }
// END CUT HERE
