Problem:
Problem D:
Identity Function
You are given an integer $N$, which is greater than 1.
Consider the following functions:
$f(a) = a^N$  mod  $N$
$F_1(a) = f(a)$
$F_{k+1}(a) = F_k(f(a))$ $(k = 1,2,3,...)$
Note that we use mod to represent the integer modulo operation. For a non-negative integer $x$
and a positive integer $y$, $x$ mod $y$ is the remainder of $x$ divided by $y$.
Output the minimum positive integer $k$ such that $F_k(a) = a$ for all positive integers $a$ less than
$N$. If no such $k$ exists, output -1.
Input
The input consists of a single line that contains an integer $N$ ($2 \leq N \leq 10^9$), whose meaning is
described in the problem statement.
Output
Output the minimum positive integer $k$ such that $F_k(a) = a$ for all positive integers $a$ less than
$N$, or -1 if no such $k$ exists.
Sample Input
3
Output for the Sample Input
1
Sample Input
4
Output for the Sample Input
-1
Sample Input
15
Output for the Sample Input
2

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
      
#define FOR(i,a,b) for( ll i = (a); i < (ll)(b); i++ )
#define REP(i,n) FOR(i,0,n)
#define YYS(x,arr) for(auto& x:arr)
#define ALL(x) (x).begin(),(x).end()
#define SORT(x) sort( (x).begin(),(x).end() )
#define REVERSE(x) reverse( (x).begin(),(x).end() )
#define UNIQUE(x) (x).erase( unique( ALL( (x) ) ) , (x).end() )
#define PW(x) (1LL<<(x))
#define SZ(x) ((ll)(x).size())
#define SHOW(x) cout << #x << " = " << x << endl
#define SHOWA(x,n) for( int yui = 0; yui < n; yui++ ){ cout << x[yui] << " "; } cout << endl

#define pb emplace_back
#define fi first
#define se second

using namespace std;

typedef long double ld;
typedef long long int ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpl> gr;
typedef vector<vl> ml;
typedef vector<vd> md;
typedef vector<vi> mi;
     
const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ld EPS = 1e-12;
const ll MOD = 1e9+7;
     
template<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }
template<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }
template<class T> inline T sq( T a ){ return a * a; }

ll in(){ long long int x; scanf( "%lld" , &x ); return x; }
char yuyushiki[1000010]; string stin(){ scanf( "%s" , yuyushiki ); return yuyushiki; }

// head

ll extgcd( ll a , ll b , ll &x , ll &y ){
  ll d = a;
  if( b != 0 ){
    d = extgcd( b , a % b , y , x );
    y -= a / b * x;
  } else {
    x = 1, y = 0;
  }
  return d;
}

ll inv( ll a , ll mod ){
  ll x, y;
  if( extgcd( a , mod , x , y ) != 1 ){
    throw 0;
  }
  x %= mod;
  if( x < 0 ){
    x += mod;
  }
  return x;
}

ll modlog( ll n , ll mod ){
  ll q = 1;
  while( q * q < mod ){
    q++;
  }
  n %= mod;
  map<ll,ll> ma;
  ll nq = 1;
  ma[1] = 0;
  REP( i , q ){
    nq = ( nq * n ) % mod;
    if( nq == 1 ){
      return i + 1;
    }
    ll a = ( inv( nq , mod ) ) % mod;
    if( ma.find( a ) == ma.end() ){
      ma[a] = i + 1;
    }
  }
  ll cur = 1;
  REP( i , q ){
    cur = ( cur * nq ) % mod;
    if( ma.find( cur ) != ma.end() ){
      return ( i + 1 ) * q + ma[cur];
    }
  }
  assert( false );
}

vl fact( ll x ){
  vl res;
  for( ll i = 2; i*i <= x; i++ ){
    while( x % i == 0 ){
      res.pb( i );
      x /= i;
    }
  }
  if( x > 1 ) res.pb( x );
  return res;
}

ll f( ll a , ll b , ll mo ){
  // a k = b mod mo
  // a k + mo l = b
  ll g = __gcd( a , mo );
  if( b % g != 0 ){
    return -1;
  }
  a /= g;
  b /= g;
  mo /= g;
  // cout << "* " << a << " " << mo << endl;
  ll k, l;
  extgcd( a , mo , k , l );
  // cout << "* " << k << " " << l << endl;
  k *= b;
  l *= b;
  k = ( ( k % mo ) + mo ) % mo;
  return k;
}

ll n;


int main(){
  
  n = in();

  vl v = fact( n );
  SORT( v );
  REP( i , SZ(v) - 1 ){
    if( v[i] == v[i+1] ){
      puts( "-1" );
      return 0;
    }
  }
  
  ll x = 1;
  ll y = 0;
  ll ans = 1;
  REP( i , SZ(v) ){
    ll mod = v[i] - 1;
    if( mod == 1 ){
      continue;
    }
    try{
      ll res = modlog( n , mod );
      if( res == -1 ){
        puts( "-1" );
        return 0;
      }
      ll r = f( x , ( ( res - y ) % mod + mod ) % mod, mod );
      // cout << r << " " << mod << endl;
      ans = ans * r / __gcd( ans , r );
      /*
      SHOW( x );
      SHOW( y );
      SHOW( res );
      SHOW( mod );
      cout << res << " " << mod << " " << r << endl;
      ll nx = x * mod;
      ll ny = x * r + y;
      x = nx;
      y = ny;
      y = ( ( y % x ) + x ) % x;
      cout << x << " " << y << endl;
      */
    } catch (...){
      puts( "-1" );
      return 0;
    }
  }

  if( ans == 0 ){
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  /*
  if( y == 0 ){
  cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
  */

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4620
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

ll phi(ll n) {
    ll ans = 1;
    ll m = n;
    for(ll i = 2; i * i <= n; i ++) {
        if(n % i == 0) {
            ans *= i - 1;
            m /= i;
            if(n % (i * i) == 0) return -1;
        }
    }
    if(m > 1) ans *= m - 1;
    return ans;
}

long long modinv(long long a, long long MOD) {
    long long b = MOD, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; std::swap(a, b);
        u -= t * v; std::swap(u, v);
    }
    u %= MOD; 
    if (u < 0) u += MOD;
    return u;
}

long long modpow(long long a, long long n, long long MOD) {
    long long res = 1;
    a %= MOD;
    if(n < 0) {
        n = -n;
        a = modinv(a, MOD);
    }
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

// depends on modpow and modinv
// a^x ≡ b (mod. m) となる最小の正の整数 x を求める
long long modlog(long long a, long long b, int m) {
    a %= m, b %= m;

    // calc sqrt{M}
    long long le = -1, ri = m;
    while(ri - le > 1) {
        long long mid = (le + ri) >> 1;
        if(mid * mid >= m) ri = mid;
        else le = mid;
    }
    long long sqrtM = ri;

    // {a^0, a^1, a^2, ..., a^sqrt(m)} 
    unordered_map<long long, long long> apow;
    long long r = a;
    for(long long i = 1; i < sqrtM; ++ i) {
        if(!apow.count(r)) apow[r] = i;
        (r *= a) %= m;
    }

    // check each A^p
    long long A = modpow(modinv(a, m), sqrtM, m);
    r = b;
    for (long long q = 0; q < sqrtM; ++q) {
        if (r == 1 && q > 0) return q * sqrtM;
        else if (apow.count(r)) return q * sqrtM + apow[r];
        (r *= A) %= m;
    }

    // no solutions
    return -1;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // 2乗以上の素因数があったらNG
    // 素数なら1
    // N=6やばい. 素因数に2があるとまずい？

    ll n; cin >> n;
    ll N = n;
    if(n == 2) {
        cout << 1 << endl;
        return 0;
    }
    ll m = phi(n);
    if(__gcd(n, m) != 1) {
        cout << -1 << endl;
        return 0;
    }
    n %= m;
    // ll ret = modlog(n, 1, m);
    ll cur = 1, ret = -1;
    for(ll i = 1; i <= 300000000; i ++) {
        cur = (cur * N) % m;
        if(cur == 1) {
            ret = i;
            break;
        }
    }
    if(ret == -1) {
        cout << -1 << endl;
        return 0;
    }
    // if(ret == )
    ll ans = ret;
    auto solve = [&](ll d, ll j) -> void {
        if(d == 0) d += j;
        if(d <= 0) return;
        
        ll a = m/2;
        ll b = min(a + 200, m - 1);
        for(ll i = a; i <= b; i ++) {
            // cout << i << " " << N * d << " " << modpow(i, N * d, N) << endl;
            if(modpow(i, modpow(N, d, j), N) != i) return;
        }
        ans = min(ans, d);
    };
    // cout << ret << endl;
    vector<ll> vec;
    for(ll i = 1; i * i <= m; i ++) {
        if(m % i == 0) {
            vec.pb(i);
            if(i * i != m) vec.pb(m / i);
            // solve(ret - i, i);
            // if(i * i != m) solve(ret - m / i, m / i);
        }
    }
    sort(all(vec));
    reverse(all(vec));
    foa(i, vec) {
        solve(ret % i, i);
        // if(ans < ret) break;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.5421686746987951
time_ms: 2800
memory_kb: 3456
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

ll phi(ll n) {
    ll ans = 1;
    ll m = n;
    for(ll i = 2; i * i <= n; i ++) {
        if(n % i == 0) {
            ans *= i - 1;
            m /= i;
            if(n % (i * i) == 0) return -1;
        }
    }
    if(m > 1) ans *= m - 1;
    return ans;
}

long long modinv(long long a, long long MOD) {
    long long b = MOD, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; std::swap(a, b);
        u -= t * v; std::swap(u, v);
    }
    u %= MOD; 
    if (u < 0) u += MOD;
    return u;
}

long long modpow(long long a, long long n, long long MOD) {
    long long res = 1;
    a %= MOD;
    if(n < 0) {
        n = -n;
        a = modinv(a, MOD);
    }
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

// depends on modpow and modinv
// a^x ≡ b (mod. m) となる最小の正の整数 x を求める
long long modlog(long long a, long long b, int m) {
    a %= m, b %= m;

    // calc sqrt{M}
    long long le = -1, ri = m;
    while(ri - le > 1) {
        long long mid = (le + ri) >> 1;
        if(mid * mid >= m) ri = mid;
        else le = mid;
    }
    long long sqrtM = ri;

    // {a^0, a^1, a^2, ..., a^sqrt(m)} 
    unordered_map<long long, long long> apow;
    long long r = a;
    for(long long i = 1; i < sqrtM; ++ i) {
        if(!apow.count(r)) apow[r] = i;
        (r *= a) %= m;
    }

    // check each A^p
    long long A = modpow(modinv(a, m), sqrtM, m);
    r = b;
    for (long long q = 0; q < sqrtM; ++q) {
        if (r == 1 && q > 0) return q * sqrtM;
        else if (apow.count(r)) return q * sqrtM + apow[r];
        (r *= A) %= m;
    }

    // no solutions
    return -1;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // 2乗以上の素因数があったらNG
    // 素数なら1
    // N=6やばい. 素因数に2があるとまずい？

    ll n; cin >> n;
    ll N = n;
    if(n == 2) {
        cout << 1 << endl;
        return 0;
    }
    ll m = phi(n);
    if(__gcd(n, m) != 1) {
        cout << -1 << endl;
        return 0;
    }
    n %= m;
    // ll ret = modlog(n, 1, m);
    ll cur = 1, ret = -1;
    for(ll i = 1; i <= 300000000; i ++) {
        cur = (cur * N) % m;
        if(cur == 1) {
            ret = i;
            break;
        }
    }
    if(ret == -1) {
        cout << -1 << endl;
        return 0;
    }
    // if(ret == )
    ll ans = ret;
    auto solve = [&](ll d, ll j) -> void {
        if(d <= 0) return;
        
        ll a = m/2;
        ll b = min(a + 200, m - 1);
        for(ll i = a; i <= b; i ++) {
            // cout << i << " " << N * d << " " << modpow(i, N * d, N) << endl;
            if(modpow(i, modpow(N, d, j), N) != i) return;
        }
        ans = min(ans, d);
    };
    // cout << ret << endl;
    vector<ll> vec;
    for(ll i = 1; i * i <= m; i ++) {
        if(m % i == 0) {
            vec.pb(i);
            if(i * i != m) vec.pb(m / i);
            // solve(ret - i, i);
            // if(i * i != m) solve(ret - m / i, m / i);
        }
    }
    sort(all(vec));
    reverse(all(vec));
    foa(i, vec) {
        solve(ret - i, i);
        if(ans < ret) break;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.5421686746987951
time_ms: 2800
memory_kb: 3488
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

ll phi(ll n) {
    ll ans = 1;
    ll m = n;
    for(ll i = 2; i * i <= n; i ++) {
        if(n % i == 0) {
            ans *= i - 1;
            m /= i;
            if(n % (i * i) == 0) return -1;
        }
    }
    if(m > 1) ans *= m - 1;
    return ans;
}

long long modinv(long long a, long long MOD) {
    long long b = MOD, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; std::swap(a, b);
        u -= t * v; std::swap(u, v);
    }
    u %= MOD; 
    if (u < 0) u += MOD;
    return u;
}

long long modpow(long long a, long long n, long long MOD) {
    long long res = 1;
    a %= MOD;
    if(n < 0) {
        n = -n;
        a = modinv(a, MOD);
    }
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

// depends on modpow and modinv
// a^x ≡ b (mod. m) となる最小の正の整数 x を求める
long long modlog(long long a, long long b, int m) {
    a %= m, b %= m;

    // calc sqrt{M}
    long long le = -1, ri = m;
    while(ri - le > 1) {
        long long mid = (le + ri) >> 1;
        if(mid * mid >= m) ri = mid;
        else le = mid;
    }
    long long sqrtM = ri;

    // {a^0, a^1, a^2, ..., a^sqrt(m)} 
    map<long long, long long> apow;
    long long r = a;
    for(long long i = 1; i < sqrtM; ++ i) {
        if(!apow.count(r)) apow[r] = i;
        (r *= a) %= m;
    }

    // check each A^p
    long long A = modpow(modinv(a, m), sqrtM, m);
    r = b;
    for (long long q = 0; q < sqrtM; ++q) {
        if (r == 1 && q > 0) return q * sqrtM;
        else if (apow.count(r)) return q * sqrtM + apow[r];
        (r *= A) %= m;
    }

    // no solutions
    return -1;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // 2乗以上の素因数があったらNG
    // 素数なら1
    // N=6やばい. 素因数に2があるとまずい？

    ll n; cin >> n;
    ll N = n;
    if(n == 2) {
        cout << 1 << endl;
        return 0;
    }
    ll m = phi(n);
    if(m == -1 or __gcd(n, m) != 1) {
        cout << -1 << endl;
        return 0;
    }
    n %= m;
    ll ret = modlog(n, 1, m);
    ll ans = ret;
    // auto solve = [&](ll d) -> void {
    //     if(d <= 0) return;
    //     ll a = m/2;
    //     ll b = min(m / 2 + 10, m - 1);
    //     for(ll i = a; i <= b; i ++) {
    //         // cout << i << " " << N * d << " " << modpow(i, N * d, N) << endl;
    //         if(modpow(i, N * d, N) != i) return;
    //     }
    //     ans = min(ans, d);
    // };
    // // cout << ret << endl;
    // for(ll i = 1; i * i <= m; i ++) {
    //     if(m % i == 0) {
    //         solve(ret - i);
    //         if(i * i != m) solve(ret - m / i);
    //     }
    // }
    cout << ans << endl;
    return 0;
}
accuracy: 0.5180722891566265
time_ms: 10
memory_kb: 6204
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

ll phi(ll n) {
    ll ans = 1;
    ll m = n;
    for(ll i = 2; i * i <= n; i ++) {
        if(n % i == 0) {
            ans *= i - 1;
            m /= i;
            if(n % (i * i) == 0) return -1;
        }
    }
    if(m > 1) ans *= m - 1;
    return ans;
}

long long modinv(long long a, long long MOD) {
    long long b = MOD, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; std::swap(a, b);
        u -= t * v; std::swap(u, v);
    }
    u %= MOD; 
    if (u < 0) u += MOD;
    return u;
}

long long modpow(long long a, long long n, long long MOD) {
    long long res = 1;
    a %= MOD;
    if(n < 0) {
        n = -n;
        a = modinv(a, MOD);
    }
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

// depends on modpow and modinv
// a^x ≡ b (mod. m) となる最小の正の整数 x を求める
long long modlog(long long a, long long b, int m) {
    a %= m, b %= m;

    // calc sqrt{M}
    long long le = -1, ri = m;
    while(ri - le > 1) {
        long long mid = (le + ri) >> 1;
        if(mid * mid >= m) ri = mid;
        else le = mid;
    }
    long long sqrtM = ri;

    // {a^0, a^1, a^2, ..., a^sqrt(m)} 
    map<long long, long long> apow;
    long long r = a;
    for(long long i = 1; i < sqrtM; ++ i) {
        if(!apow.count(r)) apow[r] = i;
        (r *= a) %= m;
    }

    // check each A^p
    long long A = modpow(modinv(a, m), sqrtM, m);
    r = b;
    for (long long q = 0; q < sqrtM; ++q) {
        if (r == 1 && q > 0) return q * sqrtM;
        else if (apow.count(r)) return q * sqrtM + apow[r];
        (r *= A) %= m;
    }

    // no solutions
    return -1;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // 2乗以上の素因数があったらNG
    // 素数なら1
    // N=6やばい. 素因数に2があるとまずい？

    ll n; cin >> n;
    if(n == 2) {
        cout << 1 << endl;
        return 0;
    }
    ll m = phi(n);
    if(m == -1 or __gcd(n, m) != 1) {
        cout << -1 << endl;
        return 0;
    }
    n %= m;
    ll ret = modlog(n, 1, m);
    cout << ret << endl;
    return 0;
}
accuracy: 0.5180722891566265
time_ms: 10
memory_kb: 6132
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long Rand() {
	long long s = 1, t = 0;
	for (int i = 0; i < 3; i++) {
		t += 1LL * (rand() % 1024) * s;
		s *= 1024;
	}
	return t;
}

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 30; i++) {
		if ((b >> i) & 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}

long long Euler(long long n) {
	long long cur = n;
	long long ans = n;
	for (int i = 2; i * i <= n; i++) {
		if (cur % i != 0) continue;
		ans = (1LL * (i - 1) * ans) / (1LL * i);
		while (cur % i == 0) cur /= i;
	}
	if (cur >= 2) ans = (ans * (cur - 1)) / cur;
	return ans;
}

long long solve(long long N) {
	if (N == 2) return 1;
	long long V = Euler(N);

	// Step 1
	long long cur = 1, ans = -1;
	for (int i = 1; i <= 300000000; i++) {
		cur = (cur * N) % V;
		if (cur == 1) { ans = i; break; }
	}
	if (ans == -1) return -1;

	// Step 2
	vector<long long> vec;
	for (int i = 1; i * i <= ans; i++) {
		if (ans % i != 0) continue;
		vec.push_back(i);
		vec.push_back(ans / i);
	}
	sort(vec.begin(), vec.end());
	vec.erase(unique(vec.begin(), vec.end()), vec.end());

	// Step 3
	for (int i = 0; i < vec.size(); i++) {
		bool flag = true;
		for (int j = 0; j < 200; j++) {
			long long num = Rand() % (N - 1) + 1;
			long long val1 = modpow(N, vec[i], V);
			long long val2 = modpow(num, val1, N);
			if (val2 != num) { flag = false; break; }
		}
		if (flag == true) return vec[i];
	}
	return -1;
}

int main() {
	long long N;
	cin >> N;
	cout << solve(N) << endl;
	return 0;
}
accuracy: 1
time_ms: 2800
memory_kb: 3424
final_rank: 6

code_snippet: #include <iostream>
using namespace std;

long long Euler(long long n) {
	long long cur = n;
	long long ans = n;
	for (int i = 2; i * i <= n; i++) {
		if (cur % i != 0) continue;
		ans = (1LL * (i - 1) * ans) / (1LL * i);
		while (cur % i == 0) cur /= i;
	}
	if (cur >= 2) ans = (ans * (cur - 1)) / cur;
	return ans;
}

long long solve(long long N) {
	if (N == 2) return 1;
	long long V = Euler(N);
	long long cur = 1;
	for (int i = 1; i <= 100000000; i++) {
		cur = (cur * N) % V;
		if (cur == 1) return i;
	}
	return -1;
}

int main() {
	long long N;
	cin >> N;
	cout << solve(N) << endl;
	return 0;
}
accuracy: 0.5421686746987951
time_ms: 930
memory_kb: 3452
final_rank: 13

code_snippet: #include <iostream>
using namespace std;

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 30; i++) {
		if ((b >> i) & 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}

long long Euler(long long n) {
	long long cur = n;
	long long ans = n;
	for (int i = 2; i * i <= n; i++) {
		if (cur % i != 0) continue;
		ans = (1LL * (i - 1) * ans) / (1LL * i);
		while (cur % i == 0) cur /= i;
	}
	if (cur >= 2) ans = (ans * (cur - 1)) / cur;
	return ans;
}

long long solve(long long N) {
	if (N == 2) return 1;
	long long V = Euler(N);
	for (int i = 1; i <= 500000; i++) {
		long long r = modpow(N, i, V);
		if (r == 1) return i;
	}
	return -1;
}

int main() {
	long long N;
	cin >> N;
	cout << solve(N) << endl;
	return 0;
}
accuracy: 0.5421686746987951
time_ms: 150
memory_kb: 3464
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int mod = 1e9 + 7;
constexpr int inf = (1 << 30) - 1;
constexpr ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr)
#define set_digit(N) cout << fixed << setprecision((N))
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

ll modlog(ll x, ll y, ll m)
{
	ll H = sqrt(m) + 1;
	static pair<ll, ll> baby[100010];
	for (ll b = 0, xby = y; b < H; b++, xby = (xby * x) % m)
	{
		baby[b] = make_pair(xby, b);
	}
	sort(baby, baby + H);
	ll xH = 1;
	for (int i = 0; i < H; ++i)
	{
		xH = (xH * x) % m;
	}
	for (ll a = 1, xaH = xH; a <= H; a++, xaH = (xaH * xH) % m)
	{
		auto it = lower_bound(baby, baby + H, make_pair(xaH + 1, 0LL));
		if (it == baby)
			continue;
		it--;
		if (it->first == xaH)
			return a * H - it->second;
	}
	return -1;
}

ll euler_phi(ll n)
{
	ll res = n;
	for (ll i = 2; i * i <= n; i++)
	{
		if (n % i == 0)
		{
			res = res / i * (i - 1);
			for (; n % i == 0; n /= i)
				;
		}
	}
	if (n != 1)
		res = res / n * (n - 1);
	return res;
}

template <typename T>
bool is_prime(T N)
{
	if (N < 2)
		return false;
	for (T i = 2; i * i <= N; ++i)
	{
		if (N % i == 0)
			return false;
	}
	return true;
}

ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }

ll mod_pow(ll x, ll n, ll mod)
{
	ll res = 1;
	while (n > 0)
	{
		if (n & 1)
			(res *= x) %= mod;
		(x *= x) %= mod;
		n >>= 1;
	}
	return res;
}

template <typename T>
vector<T> divisor(T n)
{
	vector<T> divisor;
	for (T i = 1; i * i <= n; i++)
	{
		if (n % i == 0)
		{
			divisor.push_back(i);
			if (i * i != n)
				divisor.push_back(n / i);
		}
	}
	sort(divisor.begin(), divisor.end());
	return divisor;
}

int main()
{
	ll N;
	cin >> N;
	if (is_prime(N))
	{
		cout << 1 << "\n";
		return 0;
	}

	ll phi = euler_phi(N), k = modlog(N, 1, phi);
	if (k == -1 || gcd(N, phi) > 1)
	{
		cout << -1 << "\n";
		return 0;
	}

	auto div = divisor(k);

	ll ans = k;

	for (auto &d : div)
	{
		bool ok = true;
		for (ll i = 1; i < 50000; i++)
		{
			if (mod_pow(i, mod_pow(N, d, phi), N) != i)
				ok = false;
		}
		if (ok)
		{
			ans = min(ans, d);
		}
	}
	cout << ans << "\n";
}
accuracy: 1
time_ms: 1430
memory_kb: 3940
final_rank: 5

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include<cmath>
#include <functional>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// --------------------------------------------------------------------------


ll gcd(ll a,ll b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll N;
    cin >> N;
    if(N == 6){
        cout << -1 << endl;
        return 0;
    }
    ll ori = N;
    set<ll> se;
    for(ll i=2; i<=10000000; i++){
        if(N%i == 0){
            ll cnt = 0;
            while(N%i == 0){
                cnt++;
                N /= i;
            }
            if(cnt >= 2){
                cout << -1 << endl;
                return 0;
            }
            se.emplace(i);
        }
    }
    if(N != 1){
        se.emplace(N);
    }
    vector<ll> vec;
    for(auto s: se){
        s--;
        vec.emplace_back(s);
    }
    ll g = vec[0];
    for(int i=1; i<vec.size(); i++){
        g = g * vec[i] / gcd(g,vec[i]);
    }
    set<ll> g2;
    for(ll i=1; i*i<=g; i++){
        if(g%i == 0){
            g2.emplace(i);
            g2.emplace(g/i);
        }
    }
    ll ans = 1;
    for(auto s: g2){
        s++;
        ll temp = (ori-1)%(s-1);
        if(temp == 0) temp = s-1;
        temp = ((temp*(s-1))/gcd(temp,s-1)) / temp;
        ans = (temp*ans)/gcd(ans,temp);   
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.13253012048192772
time_ms: 60
memory_kb: 3388
final_rank: 18

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include<cmath>
#include <functional>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// --------------------------------------------------------------------------


ll gcd(ll a,ll b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll N;
    cin >> N;
    ll ori = N;
    set<ll> se;
    for(ll i=2; i<=10000000; i++){
        if(N%i == 0){
            ll cnt = 0;
            while(N%i == 0){
                cnt++;
                N /= i;
            }
            if(cnt >= 2){
                cout << -1 << endl;
                return 0;
            }
            se.emplace(i);
        }
    }
    if(N != 1){
        se.emplace(N);
    }
    vector<ll> vec;
    for(auto s: se){
        s--;
        vec.emplace_back(s);
    }
    ll g = vec[0];
    for(int i=1; i<vec.size(); i++){
        g = g * vec[i] / gcd(g,vec[i]);
    }
    set<ll> g2;
    for(ll i=1; i*i<=g; i++){
        if(g%i == 0){
            g2.emplace(i);
            g2.emplace(g/i);
        }
    }
    ll ans = 1;
    for(auto s: g2){
        s++;
        ll temp = (ori-1)%(s-1);
        if(temp == 0) temp = s-1;
        temp = temp*(s-1)/gcd(temp,s-1) / temp;
        ans = temp*ans/gcd(ans,temp);   
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.08433734939759036
time_ms: 60
memory_kb: 3464
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int mod = 1e9 + 7;
constexpr int inf = (1 << 30) - 1;
constexpr ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr)
#define set_digit(N) cout << fixed << setprecision((N))
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

ll modlog(ll x, ll y, ll m)
{
	ll H = sqrt(m) + 1;
	static pair<ll, ll> baby[100010];
	for (ll b = 0, xby = y; b < H; b++, xby = (xby * x) % m)
	{
		baby[b] = make_pair(xby, b);
	}
	sort(baby, baby + H);
	ll xH = 1;
	for (int i = 0; i < H; ++i)
	{
		xH = (xH * x) % m;
	}
	for (ll a = 1, xaH = xH; a <= H; a++, xaH = (xaH * xH) % m)
	{
		auto it = lower_bound(baby, baby + H, make_pair(xaH + 1, 0LL));
		if (it == baby)
			continue;
		it--;
		if (it->first == xaH)
			return a * H - it->second;
	}
	return -1;
}

ll euler_phi(ll n)
{
	ll res = n;
	for (ll i = 2; i * i <= n; i++)
	{
		if (n % i == 0)
		{
			res = res / i * (i - 1);
			for (; n % i == 0; n /= i)
				;
		}
	}
	if (n != 1)
		res = res / n * (n - 1);
	return res;
}

template <typename T>
bool is_prime(T N)
{
	if (N < 2)
		return false;
	for (T i = 2; i * i <= N; ++i)
	{
		if (N % i == 0)
			return false;
	}
	return true;
}

ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }

ll mod_pow(ll x, ll n, ll mod)
{
	ll res = 1;
	while (n > 0)
	{
		if (n & 1)
			(res *= x) %= mod;
		(x *= x) %= mod;
		n >>= 1;
	}
	return res;
}

template <typename T>
vector<T> divisor(T n)
{
	vector<T> divisor;
	for (T i = 1; i * i <= n; i++)
	{
		if (n % i == 0)
		{
			divisor.push_back(i);
			if (i * i != n)
				divisor.push_back(n / i);
		}
	}
	sort(divisor.begin(), divisor.end());
	return divisor;
}

int main()
{
	ll N;
	cin >> N;
	if (is_prime(N))
	{
		cout << 1 << "\n";
		return 0;
	}

	ll phi = euler_phi(N), k = modlog(N, 1, phi);
	if (k == -1 || gcd(N, phi) > 1)
	{
		cout << -1 << "\n";
		return 0;
	}

	auto div = divisor(k);

	ll ans = k;

	for (auto &d : div)
	{
		bool ok = true;
		for (ll i = 1; i < 50000; i++)
		{
			if (mod_pow(i, mod_pow(N, d, phi), N) != i)
				ok = false;
		}
		if (ok)
		{
			ans = min(ans, d);
		}
	}
	cout << ans << "\n";
}
accuracy: 1
time_ms: 1420
memory_kb: 3780
final_rank: 4

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include<cmath>
#include <functional>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// --------------------------------------------------------------------------


ll gcd(ll a,ll b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll N;
    cin >> N;
    ll ori = N;
    set<ll> se;
    for(ll i=2; i<=10000000; i++){
        if(N%i == 0){
            ll cnt = 0;
            while(N%i == 0){
                cnt++;
                N /= i;
            }
            if(cnt >= 2){
                cout << -1 << endl;
                return 0;
            }
            se.emplace(i);
        }
    }
    if(N != 1){
        se.emplace(N);
    }
    ll ans = 1;
    for(auto s: se){
        ll temp = (ori-1)%(s-1);
        if(temp == 0) temp = s-1;
        temp = temp*(s-1)/gcd(temp,s-1) / temp;
        ans = temp*ans/gcd(ans,temp);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.08433734939759036
time_ms: 50
memory_kb: 3456
final_rank: 19

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long long INF = 1e18;
//const ll mod = 1000000007;
ll N;
vector<ll> f(ll x) {
    vector<ll> ret;
    for(ll i = 2; i * i <= x; i++) {
        while(x % i == 0) {
            x /= i;
            ret.push_back(i);
        }
    }
    if(x != 1) ret.push_back(x);
    return ret;
}

ll g(ll x) {
    ll now = 1;
    set<ll> st;
    for(ll t = 1; ; t++) {
        now = now * N % (x - 1);
        if(now == 1) return t;
        if(t >= x) {
            cout << -1 << endl;
            exit(0);
        }
    }
}

int main() {
    cin >> N;
    auto v = f(N);
    for(int i = 0; i + 1 < v.size(); i++) {
        if(v[i] == v[i+1]) {
            cout << -1 << endl;
            return 0;
        }
    }
    if(v.size() == 1) {
        cout << 1 << endl;
        return 0;
    }
    ll ans = 1;
    for(auto x : v) {
        ll tmp = g(x);
        ans = (ans / __gcd(ans, tmp)) * tmp;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 1550
memory_kb: 3448
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long b, long long MOD){
  long long ans = 1;
  while (b > 0){
    if (b % 2 == 1){
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    b /= 2;
  }
  return ans;
}
int gcd(int a, int b){
  if (b == 0){
    return a;
  } else {
    return gcd(b, a % b);
  }
}
int totient(int N){
  int ans = N;
  for (int i = 2; i * i <= N; i++){
    if (N % i == 0){
      ans /= i;
      ans *= i - 1;
      while (N % i == 0){
        N /= i;
      }
    }
  }
  if (N > 1){
    ans /= N;
    ans *= N - 1;
  }
  return ans;
}
int main(){
  random_device rnd;
  mt19937 mt(rnd());
  long long N;
  cin >> N;
  bool ok = true;
  for (int i = 2; i * i <= N; i++){
    if (N % (i * i) == 0){
      ok = false;
    }
  }
  if (!ok){
    cout << -1 << endl;
  } else if (N == 2){
    cout << 1 << endl;
  } else {
    long long p = totient(N);
    long long u = p * 50;
    vector<long long> f;
    for (long long i = 1; i * i <= u; i++){
      if (u % i == 0){
        if (i > 1){
          f.push_back(i);
        }
        if (i * i < u){
          f.push_back(u / i);
        }
      }
    }
    sort(f.rbegin(), f.rend());
    int cnt = f.size();
    int m = -1;
    for (int i = 0; i < cnt; i++){
      bool ok = true;
      for (int j = 0; j < 100; j++){
        int a;
        while (1){
          a = mt() % (N - 1) + 1;
          if (gcd(N, a) == 1){
            break;
          }
        }
        if (modpow(a, f[i], N) != 1){
          ok = false;
        }
      }
      if (ok){
        m = f[i];
      }
    }
    if (m == -1){
      cout << -1 << endl;
    } else {
      //N^x mod m == 1
      long long q = totient(m);
      long long v = q * 50;
      vector<long long> f2;
      for (long long i = 1; i * i <= v; i++){
        if (v % i == 0){
          f2.push_back(i);
          if (i * i < v){
            f2.push_back(v / i);
          }
        }
      }
      sort(f2.begin(), f2.end());
      int cnt2 = f2.size();
      long long ans = -1;
      for (int i = 0; i < cnt2; i++){
        if (modpow(N, f2[i], m) == 1){
          ans = f2[i];
          break;
        }
      }
      cout << ans << endl;
    }
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3248
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long b, long long MOD){
  long long ans = 1;
  while (b > 0){
    if (b % 2 == 1){
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    b /= 2;
  }
  return ans;
}
int gcd(int a, int b){
  if (b == 0){
    return a;
  } else {
    return gcd(b, a % b);
  }
}
int totient(int N){
  int ans = 1;
  for (int i = 2; i * i <= N; i++){
    if (N % i == 0){
      ans *= i - 1;
      N /= i;
    }
  }
  if (N > 1){
    ans *= N - 1;
  }
  return ans;
}
int main(){
  random_device rnd;
  mt19937 mt(rnd());
  long long N;
  cin >> N;
  bool ok = true;
  for (int i = 2; i * i <= N; i++){
    if (N % (i * i) == 0){
      ok = false;
    }
  }
  if (!ok){
    cout << -1 << endl;
  } else if (N == 2){
    cout << 1 << endl;
  } else {
    long long p = totient(N);
    long long u = p * 50;
    vector<long long> f;
    for (long long i = 1; i * i <= u; i++){
      if (u % i == 0){
        if (i > 1){
          f.push_back(i);
        }
        if (i * i < u){
          f.push_back(u / i);
        }
      }
    }
    sort(f.rbegin(), f.rend());
    int cnt = f.size();
    int m = -1;
    for (int i = 0; i < cnt; i++){
      bool ok = true;
      for (int j = 0; j < 100; j++){
        int a;
        while (1){
          a = mt() % (N - 1) + 1;
          if (gcd(N, a) == 1){
            break;
          }
        }
        if (modpow(a, f[i], N) != 1){
          ok = false;
        }
      }
      if (ok){
        m = f[i];
      }
    }
    if (m == -1){
      cout << -1 << endl;
    } else {
      //N^x mod m == 1
      long long q = totient(m);
      long long v = q * 50;
      vector<long long> f2;
      for (long long i = 1; i * i <= v; i++){
        if (v % i == 0){
          f2.push_back(i);
          if (i * i < v){
            f2.push_back(v / i);
          }
        }
      }
      sort(f2.begin(), f2.end());
      int cnt2 = f2.size();
      long long ans = -1;
      for (int i = 0; i < cnt2; i++){
        if (modpow(N, f2[i], m) == 1){
          ans = f2[i];
          break;
        }
      }
      cout << ans << endl;
    }
  }
}
accuracy: 0.6024096385542169
time_ms: 50
memory_kb: 3236
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long b, long long MOD){
  long long ans = 1;
  while (b > 0){
    if (b % 2 == 1){
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    b /= 2;
  }
  return ans;
}
int gcd(int a, int b){
  if (b == 0){
    return a;
  } else {
    return gcd(b, a % b);
  }
}
int main(){
  int N;
  cin >> N;
  bool ok = true;
  for (int i = 2; i * i <= N; i++){
    if (N % (i * i) == 0){
      ok = false;
    }
  }
  if (!ok){
    cout << -1 << endl;
  } else {
    vector<int> s;
    for (int i = 1; i < N; i++){
      if (gcd(N, i) == 1){
        s.push_back(i);
        if (s.size() >= 10){
          break;
        }
      }
    }
    int cnt = s.size();
    int ans = 0;
    vector<int> s2 = s;
    while (1){
      for (int i = 0; i < cnt; i++){
        s2[i] = modpow(s2[i], N, N);
      }
      ans++;
      if (s2 == s){
        break;
      }
      if (ans >= 1000000){
        ans = -1;
        break;
      }
    }
    cout << ans << endl;
  }
}
accuracy: 0.6385542168674698
time_ms: 3980
memory_kb: 3160
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long b, long long MOD){
  long long ans = 1;
  while (b > 0){
    if (b % 2 == 1){
      ans *= a;
      ans %= MOD;
    }
    a *= a;
    a %= MOD;
    b /= 2;
  }
  return ans;
}
int gcd(int a, int b){
  if (b == 0){
    return a;
  } else {
    return gcd(b, a % b);
  }
}
int main(){
  int N;
  cin >> N;
  bool ok = true;
  for (int i = 2; i * i <= N; i++){
    if (N % (i * i) == 0){
      ok = false;
    }
  }
  if (!ok){
    cout << -1 << endl;
  } else if (N == 2){
    cout << 1 << endl;
  } else {
    int p = 2;
    while (gcd(N, p) != 1){
      p++;
    }
    int ans = 0;
    int p2 = p;
    while (1){
      p2 = modpow(p2, N, N);
      ans++;
      if (p2 == p){
        break;
      }
      if (ans >= 1000000){
        ans = -1;
        break;
      }
    }
    cout << ans << endl;
  }
}
accuracy: 0.5421686746987951
time_ms: 390
memory_kb: 3096
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#ifdef __LOCAL
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

#ifdef __LOCAL
    #include <filesystem>
    namespace fs = std::filesystem;
#endif

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }

ll N;
void input(){
    fast_io();
    #ifdef __LOCAL
        fs::path p = __FILE__;
        fs::path input,output;
        input = output = p.parent_path();
        input += string("/input/") + string(p.stem()) + string(".txt");
        output += string("/output/") + string(p.stem()) + string(".txt");
        freopen(input.c_str(), "r", stdin);
        freopen(output.c_str(), "w", stdout);
    #endif
    cin >> N;
}

ll euler_phi(ll n) {
  ll ret = n;
  for(ll i = 2; i * i <= n; i++) {
    if(n % i == 0) {
      ret -= ret / i;
      while(n % i == 0) n /= i;
    }
  }
  if(n > 1) ret -= ret / n;
  return ret;
}


template< typename T >
T mod_pow(T x, T n, const T &p) {
  T ret = 1LL;
  while(n > 0) {
    if(n & 1) (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

map< ll, ll > prime_factor(ll n) {
  map< ll, ll > ret;
  for(ll i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}

ll __gcd(ll a,ll b){
    if(a>b) return __gcd(b,a);
    if(a==0LL) return b;
    if(a==1LL) return 1LL;
    return __gcd(b%a,a);
}

ll __lcm(ll a, ll b){
    return a*b/__gcd(a,b);
}

ll Carmichael(ll n){
    auto pf = prime_factor(n);
    // if(n==2LL) return n;
    // if(pf.size()==1) {
    //     for(auto p:pf) return (p.first-1LL);
    //     }
    if(pf.size()==1){
      for(auto p:pf){
        if(p.first==2LL){
          if(p.second<=2) return p.second;
          else return pow(2LL,(p.second-2LL));
        }
        else return (p.first-1LL)*pow(p.first,(p.second-1LL));
      }
    }
    ll ret=1LL;
    for(auto p:pf) {
        // if(p.first==2LL) ret = __lcm(ret,2LL);
        // else ret = __lcm(ret,(p.first-1LL));
        ret = __lcm(ret,Carmichael(pow(p.first,p.second)));
        }
    return ret;
}

vector< ll > divisor(ll n) {
  vector< ll > ret;
  for(ll i = 2; i * i <= n; i++) {
    if(n % i == 0) {
      ret.push_back(i);
      if(i * i != n) ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

int solve(){
    auto pf = prime_factor(N);
    // Nを素因数分解したとき、2乗以上される因子があるとき。
    for(auto p:pf) if(p.second>1) {SAY("Nを素因数分解したとき、2乗以上される因子があるとき。"); cout << "-1" << endl; return -1;}
    // Nが素数の場合
    if(pf.size()==1){SAY("Nが素数の場合") cout << 1 << endl; return 1;}
    // Nが偶数の場合
    if(N%2==0) {SAY("Nが偶数の場合") cout << "-1" << endl; return -1;}
    vector<ll> car;
    auto div = divisor(N);
    DBG(div)
    ll L=Carmichael(N);
    // for(auto p:div) car.push_back(Carmichael(p));
    // // for(auto p:pf) car.push_back(Carmichael(p.first));
    // for(auto p:pf) car.push_back(p.first-1);
    // // car.push_back(Carmichael(N));
    // // car.push_back(__lcm();
    // DBG(car)

    // Nは偶数の場合：で除かれてる。。。はず。 
    // for(auto c:car){
    //     if(N%c==0){cout << "-1" << endl; return 0;}
    // }
    // set<ll> car;
    // for(auto p:pf){
    //     ll c=p.first-1;
    //     car.insert(c);
    //     }
    vector<pll> modpows;
    // for(auto p:pf) modpows.push_back({1LL,(p.first-1LL)});
    map<ll,bool> used;
    for(auto p:div) {
      ll lamd = Carmichael(p);
      if(used[lamd]) continue;
      modpows.push_back({1LL,lamd});
      used[lamd] = true;
      }
    if(!used[L]) modpows.push_back({1LL,L});
    DBG(modpows)
    vector<ll> klcms;
    for(auto la:modpows){
      if(__gcd(N,la.second)==1LL){
        DBG(la.second)
        klcms.push_back(Carmichael(la.second));
      }
    }
    ll KLCM=1LL;
    DBG(klcms)
    for(auto lcm:klcms) KLCM = __lcm(KLCM,lcm);
    ll k=KLCM;
    DBG(KLCM)
    // while(k*k < N)
    // {
    //     bool isOk=true;
    //     for(auto &mps:modpows){
    //         mps.first *= mod_pow(N,k,mps.second);
    //         // mps.first %= mps.second;
    //         if(mps.first!=1LL){
    //             isOk=false;
    //         }
    //     }
    //     DBG(modpows)
    //     // for(auto c:car){
    //     //     if(mod_pow(N%c,k,c)!=1){
    //     //         isOk=false;
    //     //         break;
    //     //     }
    //     // }
    //     if(isOk){cout << k << endl; return k;}
    //     k+=KLCM;
    // }

    for(ll k=1LL; k <= KLCM;k++)
    {
        bool isOk=true;
        for(auto &mps:modpows){
            mps.first *= N%mps.second;
            mps.first %= mps.second;
            if(mps.first!=1LL){
                isOk=false;
            }
        }
        DBG(modpows)
        // for(auto c:car){
        //     if(mod_pow(N%c,k,c)!=1){
        //         isOk=false;
        //         break;
        //     }
        // }
        if(isOk){cout << k << endl; return k;}
    }

    SAY("その他.うまいkが見つからなかった")
    cout << "-1" << endl;
    return -1;
}   

// x^(n^k) mod m
ll mod_pow_induc(ll x, ll n, ll m, ll k){
  if(k==-1LL) return 1LL;
  if(k==0LL) return mod_pow(x,1LL,m);
  if(k==1LL) return mod_pow(x,n,m);
  else return mod_pow_induc(mod_pow(x,n,m),n,m,k-1);
}

void sample(ll N, ll ans){
  // ll po=pow(N,ans)
  // llだと桁が足りない。
  if(ans==-1) return;
    for (ll i = 1; i < min(N/ans,(ll)100); i++)
    {
      // iとNが互いに素
      // ll po;
      // ll m=__gcd(N,i);
      // po = mod_pow(N,ans,Carmichael(N/m));
      // DBG(po)
      // 微妙に違う・・・
      // else po=mod_pow(N,ans,Carmichael(i));
      // cout << mod_pow(i,po,N) << endl;
      cout << mod_pow_induc(i,N,N,ans) << endl;
    }
    
}

int main()
{
    input();
    // N=1234567;
    ll ans = solve();
    // sample(N, ans);
    return 0;
}
accuracy: 1
time_ms: 4850
memory_kb: 3356
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#ifdef __LOCAL
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

#ifdef __LOCAL
    #include <filesystem>
    namespace fs = std::filesystem;
#endif

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }

ll N;
void input(){
    // fast_io();/
    #ifdef __LOCAL
        fs::path p = __FILE__;
        fs::path input,output;
        input = output = p.parent_path();
        input += string("/input/") + string(p.stem()) + string(".txt");
        output += string("/output/") + string(p.stem()) + string(".txt");
        freopen(input.c_str(), "r", stdin);
        freopen(output.c_str(), "w", stdout);
    #endif
    cin >> N;
}

ll euler_phi(ll n) {
  ll ret = n;
  for(ll i = 2; i * i <= n; i++) {
    if(n % i == 0) {
      ret -= ret / i;
      while(n % i == 0) n /= i;
    }
  }
  if(n > 1) ret -= ret / n;
  return ret;
}


template< typename T >
T mod_pow(T x, T n, const T &p) {
  T ret = 1LL;
  while(n > 0) {
    if(n & 1) (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}

map< ll, ll > prime_factor(ll n) {
  map< ll, ll > ret;
  for(ll i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}

ll __gcd(ll a,ll b){
    if(a>b) return __gcd(b,a);
    if(a==0LL) return b;
    if(a==1LL) return 1LL;
    return __gcd(b%a,a);
}

ll __lcm(ll a, ll b){
    return a*b/__gcd(a,b);
}

ll Carmichael(ll n){
    auto pf = prime_factor(n);
    // if(n==2LL) return n;
    // if(pf.size()==1) {
    //     for(auto p:pf) return (p.first-1LL);
    //     }
    ll ret=1LL;
    for(auto p:pf) {
        if(p.first==2LL) ret = __lcm(ret,2LL);
        else ret = __lcm(ret,(p.first-1LL));
        // ret = __lcm(ret,Carmichael(p.first));
        }
    return ret;
}

vector< ll > divisor(ll n) {
  vector< ll > ret;
  for(ll i = 2; i * i <= n; i++) {
    if(n % i == 0) {
      ret.push_back(i);
      if(i * i != n) ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

int solve(){
    auto pf = prime_factor(N);
    // Nを素因数分解したとき、2乗以上される因子があるとき。
    for(auto p:pf) if(p.second>1) {cout << "-1" << endl; return 0;}
    // Nが素数の場合
    if(pf.size()==1){cout << 1 << endl; return 0;}
    // Nが偶数の場合
    if(N%2==0) {cout << "-1" << endl; return 0;}
    vector<ll> car;
    auto div = divisor(N);
    DBG(div)
    ll L=Carmichael(N);
    // for(auto p:div) car.push_back(Carmichael(p));
    // // for(auto p:pf) car.push_back(Carmichael(p.first));
    // for(auto p:pf) car.push_back(p.first-1);
    // // car.push_back(Carmichael(N));
    // // car.push_back(__lcm();
    // DBG(car)

    // Nは偶数の場合：で除かれてる。。。はず。 
    // for(auto c:car){
    //     if(N%c==0){cout << "-1" << endl; return 0;}
    // }
    // set<ll> car;
    // for(auto p:pf){
    //     ll c=p.first-1;
    //     car.insert(c);
    //     }
    vector<pll> modpows;
    // for(auto p:pf) modpows.push_back({1LL,(p.first-1LL)});
    map<ll,bool> used;
    for(auto p:div) {
      ll lamd = Carmichael(p);
      if(used[lamd]) continue;
      modpows.push_back({1LL,lamd});
      used[lamd] = true;
      }
    if(!used[L]) modpows.push_back({1LL,L});
    DBG(modpows)

    for (ll k = 1; k < L; k++)
    {
        bool isOk=true;
        for(auto &mps:modpows){
            mps.first *= (N%mps.second);
            mps.first %= mps.second;
            if(mps.first!=1LL){
                isOk=false;
                break;
            }
        }
        // for(auto c:car){
        //     if(mod_pow(N%c,k,c)!=1){
        //         isOk=false;
        //         break;
        //     }
        // }
        if(isOk){cout << k << endl; return 0;}
    }
    cout << "-1" << endl;
    return 0;
}   

void sample(ll N=N){
    ll po=pow(N,6);
    for (ll i = 0; i < N; i++)
    {
        cout << mod_pow(i,6LL,N) << endl;
    }
    
}

int main()
{
    // N=21;
    input();
    solve();
    // sample(21);
    // for (int i = 1; i < 15; i++)
    // {
    //     cout << mod_pow(i,4,15) << endl;
    // }    
    return 0;
}
accuracy: 0.5421686746987951
time_ms: 300
memory_kb: 3140
final_rank: 10