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 C (Clang 3.8.0) (judge verdict: Runtime Error)

#include <math.h>
#include <stdio.h>
#include <stdlib.h>


struct list{
    int value;
    struct list* prev;
    struct list* next;
};

struct list* new_list(int n){
    struct list* p = (struct list*)malloc(sizeof(struct list));

    if (p == NULL) {
        fprintf(stderr, "Fail creating list");
        exit(1);
    }

    p->value = n;
    p->prev = NULL;
    p->next = NULL;

    return p;
}

struct list* last(struct list* list_head){
    while (list_head->next != NULL) list_head = list_head->next;
    return list_head;
}

struct list* append(struct list* list_head, int n){
    struct list* p = new_list(n);

    last(list_head)->next = p;

    return list_head;
}

int len_list(struct list* list_head){
    int len = 1;
    while (list_head->next != NULL) len++;
    return len;
}

int does_val_exist(struct list* list_head, int n){
    if (list_head->value == n) return 1;
    while (list_head->next != NULL) {
        list_head = list_head->next;
        if (list_head->value == n) return 1;
    }
    return 0;
}

struct list* list_divisor(int n){
    struct list* divisor = new_list(1);

    for(int i = 2; i <= sqrt(n); i++){
        if (n % i == 0) append(divisor, i);
    }

    struct list* p = last(divisor);
    int sqrt_of_n = (int)sqrt(n);
    if (n == sqrt_of_n * sqrt_of_n) p->next = new_list(sqrt_of_n);
    for(;p->prev != NULL; p = p->prev){
        append(divisor, n / p->value);
    }

    return divisor;
}

int main(void){
    int n;
    scanf("%d\n", &n);

    int sub_only = len_list(list_divisor(n - 1)) - 1;

    int div_included = 0;
    for (struct list* p_divisor = list_divisor(n)->next; p_divisor->next != NULL; p_divisor = p_divisor->next){
        int n1 = n / p_divisor->value;
        while (n1 % p_divisor->value == 0) n1 /= p_divisor->value;
        
        if (n1 == 1){
            div_included++;
        } else if (does_val_exist(list_divisor(n1 - 1), p_divisor->value)){
            div_included++;
        }
    }
    
    printf("%d\n", sub_only + div_included);
    return 0;
}