






"""simple docstring"""







import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModelWithProjection

from diffusers import (
    AutoencoderKL,
    DPMSolverMultistepScheduler,
    PNDMScheduler,
    StableDiffusionImageVariationPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, nightly, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


class     __UpperCAmelCase(   SCREAMING_SNAKE_CASE__  ,  SCREAMING_SNAKE_CASE__  ,  SCREAMING_SNAKE_CASE__  ,  unittest.TestCase							):




						"""simple docstring"""




						__lowerCamelCase				       =					StableDiffusionImageVariationPipeline
						__lowerCamelCase				       =					IMAGE_VARIATION_PARAMS
						__lowerCamelCase				       =					IMAGE_VARIATION_BATCH_PARAMS
						__lowerCamelCase				       =					frozenset([]							)
						# TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
						__lowerCamelCase				       =					frozenset([]							)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												torch.manual_seed(0			)
												lowercase__				:      List[str]=       UNetaDConditionModel(
												    block_out_channels=(32, 64)       ,  layers_per_block=2       ,  sample_size=32       ,  in_channels=4       ,  out_channels=4       ,  down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")       ,  up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")       ,  cross_attention_dim=32       ,  )
												lowercase__				:      int=       PNDMScheduler(skip_prk_steps=snake_case__			)
												torch.manual_seed(0			)
												lowercase__				:      Dict=       AutoencoderKL(
												    block_out_channels=[32, 64]       ,  in_channels=3       ,  out_channels=3       ,  down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]       ,  up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]       ,  latent_channels=4       ,  )
												torch.manual_seed(0			)
												lowercase__				:      str=       CLIPVisionConfig(
												    hidden_size=32       ,  projection_dim=32       ,  intermediate_size=37       ,  layer_norm_eps=1e-05       ,  num_attention_heads=4       ,  num_hidden_layers=5       ,  image_size=32       ,  patch_size=4       ,  )
												lowercase__				:      Optional[Any]=       CLIPVisionModelWithProjection(snake_case__			)
												lowercase__				:      Union[str, Any]=       CLIPImageProcessor(crop_size=32       ,  size=32			)

												lowercase__				:      Optional[int]=       {
												    "unet": unet,
												    "scheduler": scheduler,
												    "vae": vae,
												    "image_encoder": image_encoder,
												    "feature_extractor": feature_extractor,
												    "safety_checker": None,
												}
												return components




						def 		UpperCAmelCase_       (   self       ,  snake_case__       ,  snake_case__=0			):
												'''simple docstring'''
												lowercase__				:      List[str]=       floats_tensor((1, 3, 32, 32)       ,  rng=random.Random(snake_case__			)			)
												lowercase__				:      Dict=       image.cpu().permute(0       ,  2       ,  3       ,  1			)[0]
												lowercase__				:      Optional[Any]=       Image.fromarray(np.uinta(snake_case__			)			).convert("RGB"			).resize((32, 32)			)
												if str(snake_case__			).startswith("mps"			):
																		lowercase__				:      Any=       torch.manual_seed(snake_case__			)
												else:
																		lowercase__				:      Any=       torch.Generator(device=snake_case__			).manual_seed(snake_case__			)
												lowercase__				:      Dict=       {
												    "image": image,
												    "generator": generator,
												    "num_inference_steps": 2,
												    "guidance_scale": 6.0,
												    "output_type": "numpy",
												}
												return inputs




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      List[Any]=       "cpu"  # ensure determinism for the device-dependent torch.Generator
												lowercase__				:      Dict=       self.get_dummy_components()
												lowercase__				:      Optional[int]=       StableDiffusionImageVariationPipeline(**snake_case__			)
												lowercase__				:      int=       sd_pipe.to(snake_case__			)
												sd_pipe.set_progress_bar_config(disable=snake_case__			)

												lowercase__				:      Any=       self.get_dummy_inputs(snake_case__			)
												lowercase__				:      List[Any]=       sd_pipe(**snake_case__			).images
												lowercase__				:      List[str]=       image[0, -3:, -3:, -1]

												assert image.shape == (1, 64, 64, 3)
												lowercase__				:      Dict=       np.array([0.52_39, 0.57_23, 0.47_96, 0.50_49, 0.55_50, 0.46_85, 0.53_29, 0.48_91, 0.49_21]			)

												assert np.abs(image_slice.flatten() - expected_slice			).max() < 1e-3




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Any=       "cpu"  # ensure determinism for the device-dependent torch.Generator
												lowercase__				:      Optional[Any]=       self.get_dummy_components()
												lowercase__				:      Optional[Any]=       StableDiffusionImageVariationPipeline(**snake_case__			)
												lowercase__				:      Union[str, Any]=       sd_pipe.to(snake_case__			)
												sd_pipe.set_progress_bar_config(disable=snake_case__			)

												lowercase__				:      Optional[int]=       self.get_dummy_inputs(snake_case__			)
												lowercase__				:      Optional[int]=       2 * [inputs["image"]]
												lowercase__				:      Tuple=       sd_pipe(**snake_case__			)

												lowercase__				:      str=       output.images

												lowercase__				:      Optional[int]=       image[-1, -3:, -3:, -1]

												assert image.shape == (2, 64, 64, 3)
												lowercase__				:      Optional[Any]=       np.array([0.68_92, 0.56_37, 0.58_36, 0.57_71, 0.62_54, 0.64_09, 0.55_80, 0.55_69, 0.52_89]			)

												assert np.abs(image_slice.flatten() - expected_slice			).max() < 1e-3




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												super().test_inference_batch_single_identical(expected_max_diff=3e-3			)


@slow
@require_torch_gpu
class     __UpperCAmelCase(   unittest.TestCase							):




						"""simple docstring"""




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()




						def 		UpperCAmelCase_       (   self       ,  snake_case__       ,  snake_case__="cpu"       ,  snake_case__=torch.floataa       ,  snake_case__=0			):
												'''simple docstring'''
												lowercase__				:      Tuple=       torch.Generator(device=snake_case__			).manual_seed(snake_case__			)
												lowercase__				:      int=       load_image(
												    "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
												    "/stable_diffusion_imgvar/input_image_vermeer.png"			)
												lowercase__				:      Optional[Any]=       np.random.RandomState(snake_case__			).standard_normal((1, 4, 64, 64)			)
												lowercase__				:      str=       torch.from_numpy(snake_case__			).to(device=snake_case__       ,  dtype=snake_case__			)
												lowercase__				:      List[Any]=       {
												    "image": init_image,
												    "latents": latents,
												    "generator": generator,
												    "num_inference_steps": 3,
												    "guidance_scale": 7.5,
												    "output_type": "numpy",
												}
												return inputs




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Optional[Any]=       StableDiffusionImageVariationPipeline.from_pretrained(
												    "lambdalabs/sd-image-variations-diffusers"       ,  safety_checker=snake_case__			)
												lowercase__				:      str=       sd_pipe.to(snake_case__			)
												sd_pipe.set_progress_bar_config(disable=snake_case__			)

												lowercase__				:      List[Any]=       self.get_inputs(snake_case__			)
												lowercase__				:      List[str]=       sd_pipe(**snake_case__			).images
												lowercase__				:      Any=       image[0, -3:, -3:, -1].flatten()

												assert image.shape == (1, 512, 512, 3)
												lowercase__				:      Union[str, Any]=       np.array([0.8_44_91, 0.9_07_89, 0.7_57_08, 0.7_87_34, 0.8_34_85, 0.7_00_99, 0.6_69_38, 0.6_87_27, 0.6_13_79]			)
												assert np.abs(image_slice - expected_slice			).max() < 6e-3




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      str=       0

												def callback_fn(snake_case__       ,  snake_case__       ,  snake_case__			) -> None:
																		lowercase__				:      int=       True
																		nonlocal number_of_steps
																		number_of_steps += 1
																		if step == 1:
																								lowercase__				:      Any=       latents.detach().cpu().numpy()
																								assert latents.shape == (1, 4, 64, 64)
																								lowercase__				:      int=       latents[0, -3:, -3:, -1]
																								lowercase__				:      Union[str, Any]=       np.array(
																								    [-0.16_21, 0.28_37, -0.79_79, -0.12_21, -1.30_57, 0.76_81, -2.11_91, 0.04_64, 1.63_09]			)

																								assert np.abs(latents_slice.flatten() - expected_slice			).max() < 5e-2
																		elif step == 2:
																								lowercase__				:      List[Any]=       latents.detach().cpu().numpy()
																								assert latents.shape == (1, 4, 64, 64)
																								lowercase__				:      Optional[int]=       latents[0, -3:, -3:, -1]
																								lowercase__				:      List[str]=       np.array([0.62_99, 1.75_00, 1.19_92, -2.15_82, -1.89_94, 0.73_34, -0.70_90, 1.01_37, 1.52_73]			)

																								assert np.abs(latents_slice.flatten() - expected_slice			).max() < 5e-2

												lowercase__				:      Dict=       False

												lowercase__				:      List[str]=       StableDiffusionImageVariationPipeline.from_pretrained(
												    "fusing/sd-image-variations-diffusers"       ,  safety_checker=snake_case__       ,  torch_dtype=torch.floataa       ,  )
												pipe.to(snake_case__			)
												pipe.set_progress_bar_config(disable=snake_case__			)
												pipe.enable_attention_slicing()

												lowercase__				:      List[str]=       self.get_inputs(snake_case__       ,  dtype=torch.floataa			)
												pipe(**snake_case__       ,  callback=snake_case__       ,  callback_steps=1			)
												assert callback_fn.has_been_called
												assert number_of_steps == inputs["num_inference_steps"]




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												torch.cuda.empty_cache()
												torch.cuda.reset_max_memory_allocated()
												torch.cuda.reset_peak_memory_stats()

												lowercase__				:      List[Any]=       "fusing/sd-image-variations-diffusers"
												lowercase__				:      Dict=       StableDiffusionImageVariationPipeline.from_pretrained(
												    snake_case__       ,  safety_checker=snake_case__       ,  torch_dtype=torch.floataa			)
												lowercase__				:      Optional[int]=       pipe.to(snake_case__			)
												pipe.set_progress_bar_config(disable=snake_case__			)
												pipe.enable_attention_slicing(1			)
												pipe.enable_sequential_cpu_offload()

												lowercase__				:      Optional[Any]=       self.get_inputs(snake_case__       ,  dtype=torch.floataa			)
												lowercase__				:      int=       pipe(**snake_case__			)

												lowercase__				:      Tuple=       torch.cuda.max_memory_allocated()
												# make sure that less than 2.6 GB is allocated
												assert mem_bytes < 2.6 * 10**9


@nightly
@require_torch_gpu
class     __UpperCAmelCase(   unittest.TestCase							):




						"""simple docstring"""




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()




						def 		UpperCAmelCase_       (   self       ,  snake_case__       ,  snake_case__="cpu"       ,  snake_case__=torch.floataa       ,  snake_case__=0			):
												'''simple docstring'''
												lowercase__				:      str=       torch.Generator(device=snake_case__			).manual_seed(snake_case__			)
												lowercase__				:      List[Any]=       load_image(
												    "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
												    "/stable_diffusion_imgvar/input_image_vermeer.png"			)
												lowercase__				:      List[str]=       np.random.RandomState(snake_case__			).standard_normal((1, 4, 64, 64)			)
												lowercase__				:      Optional[Any]=       torch.from_numpy(snake_case__			).to(device=snake_case__       ,  dtype=snake_case__			)
												lowercase__				:      List[Any]=       {
												    "image": init_image,
												    "latents": latents,
												    "generator": generator,
												    "num_inference_steps": 50,
												    "guidance_scale": 7.5,
												    "output_type": "numpy",
												}
												return inputs




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      List[Any]=       StableDiffusionImageVariationPipeline.from_pretrained("fusing/sd-image-variations-diffusers"			)
												sd_pipe.to(snake_case__			)
												sd_pipe.set_progress_bar_config(disable=snake_case__			)

												lowercase__				:      Dict=       self.get_inputs(snake_case__			)
												lowercase__				:      Tuple=       sd_pipe(**snake_case__			).images[0]

												lowercase__				:      str=       load_numpy(
												    "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
												    "/stable_diffusion_imgvar/lambdalabs_variations_pndm.npy"			)
												lowercase__				:      Any=       np.abs(expected_image - image			).max()
												assert max_diff < 1e-3




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      List[str]=       StableDiffusionImageVariationPipeline.from_pretrained("fusing/sd-image-variations-diffusers"			)
												lowercase__				:      Any=       DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config			)
												sd_pipe.to(snake_case__			)
												sd_pipe.set_progress_bar_config(disable=snake_case__			)

												lowercase__				:      int=       self.get_inputs(snake_case__			)
												lowercase__				:      str=       25
												lowercase__				:      Union[str, Any]=       sd_pipe(**snake_case__			).images[0]

												lowercase__				:      Optional[Any]=       load_numpy(
												    "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
												    "/stable_diffusion_imgvar/lambdalabs_variations_dpm_multi.npy"			)
												lowercase__				:      Tuple=       np.abs(expected_image - image			).max()
												assert max_diff < 1e-3














"""simple docstring"""







def 						lowercase__(A = 1_000						)				->int:




						"""simple docstring"""



						lowercase__				:      Any=       [0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
						# number of letters in twenty, thirty, ..., ninety (0 for numbers less than
						# 20 due to inconsistency in teens)
						lowercase__				:      List[str]=       [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]

						lowercase__				:      Dict=       0

						for i in range(1					,		n + 1						):
												if i < 1_000:
																		if i >= 100:
																								# add number of letters for "n hundred"
																								count += ones_counts[i // 100] + 7

																								if i % 100 != 0:
																														# add number of letters for "and" if number is not multiple
																														# of 100
																														count += 3

																		if 0 < i % 100 < 20:
																								# add number of letters for one, two, three, ..., nineteen
																								# (could be combined with below if not for inconsistency in
																								# teens)
																								count += ones_counts[i % 100]
																		else:
																								# add number of letters for twenty, twenty one, ..., ninety
																								# nine
																								count += ones_counts[i % 10]
																								count += tens_counts[(i % 100 - i % 10) // 10]
												else:
																		count += ones_counts[i // 1_000] + 8
						return count


if __name__ == "__main__":
			print(solution(int(input().strip())))






