let for_range s e step f =
  let rec loop i =
    if i > e then ()
    else
      begin f i;loop (i + step) end
  in loop s;;

let () =
  let num = 1000000 in
  let p = Array.make (num+1) 1 in
  let rec loop i =
    if i*i < num then
      begin
        if p.(i) = 1 then
          begin
            p.(i*2) <- 0;for_range (i*3) num (i*2) (fun x -> p.(x) <- 0);
          end;
        loop (i+2)
      end
  in
  p.(0) <- 0; p.(1)<-(0);
  for_range 4 num 2 (fun x -> p.(x) <- 0);loop 3;
  let get_count n =
    let count = ref 0 in
    for i = 1 to (n+(n mod 2))/2 do
      if p.(i) = 1 && p.(n-i+1) = 1 then
        if i = n-i+1 then
          count := !count+1
        else
          count := !count+2
    done;
    count
  in
  let rec read () =
    try let n = read_int () in
        Printf.printf "%d\n" !(get_count n);
        read ()
    with End_of_file -> ()
  in read ();;