Problem:
Score : 600 points

Problem Statement

Given is a positive integer N.

We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K.

Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K.

In how many choices of K will N become 1 in the end?

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of choices of K in which N becomes 1 in the end.

Sample Input 1

6

Sample Output 1

3

There are three choices of K in which N becomes 1 in the end: 2, 5, and 6.

In each of these choices, N will change as follows:

When K=2: 6 \to 3 \to 1

When K=5: 6 \to 1

When K=6: 6 \to 1

Sample Input 2

3141

Sample Output 2

13

Sample Input 3

314159265358

Sample Output 3

9

Solution in Kotlin (1.0.0) (judge verdict: Wrong Answer)

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.lang.StringBuilder
import java.util.*

fun gcd(a:Int, b:Int):Int {
    val c = a % b
    if (c == 0) return b
    return gcd(b, c)
}

fun PrintWriter.solve(sc: FastScanner) {
    val n = sc.nextLong()
    val fac = mutableMapOf<Long, Int>()
    var d = 2L
    var m = n
    while (d * d <= m) {
        if (m % d == 0L) {
            var count = 0
            while (m % d == 0L) {
                m /= d
                count++
            }
            fac[d] = count
        }
        d++
    }
    if (m != 1L) {
        fac[m] = 1
    }
    val prime = fac.keys.toList()
    val l = prime.count()
    var ans = 0L
    d = 1L
    while (d * d <= n - 1) {
        if ((n-1) % d == 0L) {
            ans++
            if (d * d != n - 1) {
                ans++
            }
        }
        d++
    }
    for (i in 0 until 1L.shl(l)) {
        var ak1 = 1L
        var kb = 1L
        var gcd = 0
        for (j in 0 until l) {
            if ((1L.shl(j) and i) != 0L) {
                ak1 *= Math.pow(prime[j].toDouble(), fac[prime[j]]!!.toDouble()).toLong()
            } else {
                kb *= Math.pow(prime[j].toDouble(), fac[prime[j]]!!.toDouble()).toLong()
                gcd = gcd(gcd, fac[prime[j]]!!)
            }
        }
        val div = mutableListOf<Int>()
        var d = 1
        while (d * d <= gcd) {
            if (gcd % d == 0) {
                div.add(d)
                if (d * d != gcd) {
                    div.add(gcd / d)
                }
            }
            d++
        }
        for (b in div) {
            val k = Math.pow(kb.toDouble(), 1.0 / b).toLong()
            if (ak1 % k == 1L) {
                ans++
            }
        }
    }
    if (l == 1) ans++
    println(ans - 1)
}

fun main(args: Array<String>) {
    val writer = PrintWriter(System.out, false)
    writer.solve(FastScanner(System.`in`))
    writer.flush()
}

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    fun next(): String {
        while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())

        return st.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextLine() = br.readLine()
    fun nextDouble() = next().toDouble()
    fun ready() = br.ready()
}