Function to count pages