while line = gets
  b = 0
  n = line.to_i
  primes = (2..Math.sqrt(n)).inject([*2..n]) do |nums, sieve|
    nums.reject { |num| (num % sieve).zero? && !num.eql?(sieve) }
  end

  if primes.empty?
    puts b
    next
  end

  a = (1..n).to_a
  b = 0
  i = 0
  while i < a.length
    b += 1 if primes.include?(a[i]) && primes.include?(a.reverse[i])
    i += 1
  end
  puts b
end