

'''simple docstring'''




import math
import os
from copy import deepcopy

import datasets
import evaluate
import torch
import transformers
from datasets import load_dataset
from torch.utils.data import DataLoader
from transformers import AutoModelForSequenceClassification, AutoTokenizer

from accelerate import Accelerator
from accelerate.test_utils import RegressionDataset, RegressionModel
from accelerate.utils import is_tpu_available, set_seed


_a					:			Tuple							=    'true'


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : str							, SCREAMING_SNAKE_CASE   : Optional[Any]=82							, SCREAMING_SNAKE_CASE   : Optional[int]=16    ):
  set_seed(42    )
  UpperCAmelCase    				=						RegressionModel()
  UpperCAmelCase    				=						deepcopy(SCREAMING_SNAKE_CASE    )
  UpperCAmelCase    				=						RegressionDataset(length=SCREAMING_SNAKE_CASE    )
  UpperCAmelCase    				=						DataLoader(SCREAMING_SNAKE_CASE							, batch_size=SCREAMING_SNAKE_CASE    )
  model.to(accelerator.device    )
  UpperCAmelCase				,   UpperCAmelCase    				=						accelerator.prepare(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )
  return model, ddp_model, dataloader


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : Accelerator							, SCREAMING_SNAKE_CASE   : Dict=False    ):
  UpperCAmelCase    				=						AutoTokenizer.from_pretrained('hf-internal-testing/mrpc-bert-base-cased'    )
  UpperCAmelCase    				=						load_dataset('glue'							, 'mrpc'							, split='validation'    )

  def tokenize_function(SCREAMING_SNAKE_CASE   : Optional[Any]    ):
    UpperCAmelCase    				=						tokenizer(examples['sentence1']							, examples['sentence2']							, truncation=SCREAMING_SNAKE_CASE							, max_length=SCREAMING_SNAKE_CASE    )
    return outputs

  with accelerator.main_process_first():
    UpperCAmelCase    				=						dataset.map(
        SCREAMING_SNAKE_CASE							, batched=SCREAMING_SNAKE_CASE							, remove_columns=['idx', 'sentence1', 'sentence2']							, )

  UpperCAmelCase    				=						tokenized_datasets.rename_column('label'							, 'labels'    )

  def collate_fn(SCREAMING_SNAKE_CASE   : List[str]    ):
    if use_longest:
      return tokenizer.pad(SCREAMING_SNAKE_CASE							, padding='longest'							, return_tensors='pt'    )
    return tokenizer.pad(SCREAMING_SNAKE_CASE							, padding='max_length'							, max_length=128							, return_tensors='pt'    )

  return DataLoader(SCREAMING_SNAKE_CASE							, shuffle=SCREAMING_SNAKE_CASE							, collate_fn=SCREAMING_SNAKE_CASE							, batch_size=16    )


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : Optional[Any]							, SCREAMING_SNAKE_CASE   : Dict    ):
  UpperCAmelCase    				=						Accelerator(dispatch_batches=SCREAMING_SNAKE_CASE							, split_batches=SCREAMING_SNAKE_CASE    )
  UpperCAmelCase    				=						get_dataloader(SCREAMING_SNAKE_CASE							, not dispatch_batches    )
  UpperCAmelCase    				=						AutoModelForSequenceClassification.from_pretrained(
      'hf-internal-testing/mrpc-bert-base-cased'							, return_dict=SCREAMING_SNAKE_CASE    )
  UpperCAmelCase				,   UpperCAmelCase    				=						accelerator.prepare(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )
  return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : List[Any]							, SCREAMING_SNAKE_CASE   : Dict							, SCREAMING_SNAKE_CASE   : Union[str, Any]    ):
  UpperCAmelCase    				=						[]
  for batch in dataloader:
    UpperCAmelCase				,   UpperCAmelCase    				=						batch.values()
    with torch.no_grad():
      UpperCAmelCase    				=						model(SCREAMING_SNAKE_CASE    )
      UpperCAmelCase				,   UpperCAmelCase    				=						accelerator.gather_for_metrics((logit, target)    )
      logits_and_targets.append((logit, target)    )
  UpperCAmelCase				,   UpperCAmelCase    				=						[], []
  for logit, targ in logits_and_targets:
    logits.append(SCREAMING_SNAKE_CASE    )
    targs.append(SCREAMING_SNAKE_CASE    )
  UpperCAmelCase				,   UpperCAmelCase    				=						torch.cat(SCREAMING_SNAKE_CASE    ), torch.cat(SCREAMING_SNAKE_CASE    )
  return logits, targs


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : Accelerator							, SCREAMING_SNAKE_CASE   : Union[str, Any]=82							, SCREAMING_SNAKE_CASE   : List[str]=False							, SCREAMING_SNAKE_CASE   : List[Any]=False							, SCREAMING_SNAKE_CASE   : Optional[Any]=16    ):
  UpperCAmelCase				,   UpperCAmelCase				,   UpperCAmelCase    				=						get_basic_setup(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )
  UpperCAmelCase				,   UpperCAmelCase    				=						generate_predictions(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )
  assert (
      len(SCREAMING_SNAKE_CASE    ) == num_samples
  ), f'''Unexpected number of inputs:\n    Expected: {num_samples}\n    Actual: {len(SCREAMING_SNAKE_CASE    )}'''


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : bool = False							, SCREAMING_SNAKE_CASE   : bool = False    ):
  UpperCAmelCase    				=						evaluate.load('glue'							, 'mrpc'    )
  UpperCAmelCase				,   UpperCAmelCase    				=						get_mrpc_setup(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )
  # First do baseline
  UpperCAmelCase				,   UpperCAmelCase				,   UpperCAmelCase    				=						setup['no']
  model.to(SCREAMING_SNAKE_CASE    )
  model.eval()
  for batch in dataloader:
    batch.to(SCREAMING_SNAKE_CASE    )
    with torch.inference_mode():
      UpperCAmelCase    				=						model(**SCREAMING_SNAKE_CASE    )
    UpperCAmelCase    				=						outputs.logits.argmax(dim=-1    )
    metric.add_batch(predictions=SCREAMING_SNAKE_CASE							, references=batch['labels']    )
  UpperCAmelCase    				=						metric.compute()

  # Then do distributed
  UpperCAmelCase				,   UpperCAmelCase				,   UpperCAmelCase    				=						setup['ddp']
  model.eval()
  for batch in dataloader:
    with torch.inference_mode():
      UpperCAmelCase    				=						model(**SCREAMING_SNAKE_CASE    )
    UpperCAmelCase    				=						outputs.logits.argmax(dim=-1    )
    UpperCAmelCase    				=						batch['labels']
    UpperCAmelCase				,   UpperCAmelCase    				=						accelerator.gather_for_metrics((preds, references)    )
    metric.add_batch(predictions=SCREAMING_SNAKE_CASE							, references=SCREAMING_SNAKE_CASE    )
  UpperCAmelCase    				=						metric.compute()

  for key in "accuracy f1".split():
    assert math.isclose(
        baseline[key]							, distributed[key]    ), f'''Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n'''


def    lowerCamelCase__		(    ):
  UpperCAmelCase    				=						Accelerator(split_batches=SCREAMING_SNAKE_CASE							, dispatch_batches=SCREAMING_SNAKE_CASE    )
  if accelerator.is_local_main_process:
    datasets.utils.logging.set_verbosity_warning()
    transformers.utils.logging.set_verbosity_warning()
  else:
    datasets.utils.logging.set_verbosity_error()
    transformers.utils.logging.set_verbosity_error()
  # These are a bit slower so they should only be ran on the GPU or TPU
  if torch.cuda.is_available() or is_tpu_available():
    if accelerator.is_local_main_process:
      print('**Testing gather_for_metrics**'    )
    for split_batches in [True, False]:
      for dispatch_batches in [True, False]:
        if accelerator.is_local_main_process:
          print(f'''With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`'''    )
        test_mrpc(SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE    )
        accelerator.state._reset_state()
  if accelerator.is_local_main_process:
    print('**Test torch metrics**'    )
  for split_batches in [True, False]:
    for dispatch_batches in [True, False]:
      UpperCAmelCase    				=						Accelerator(split_batches=SCREAMING_SNAKE_CASE							, dispatch_batches=SCREAMING_SNAKE_CASE    )
      if accelerator.is_local_main_process:
        print(f'''With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99'''    )
      test_torch_metrics(SCREAMING_SNAKE_CASE							, 99    )
      accelerator.state._reset_state()
  if accelerator.is_local_main_process:
    print('**Test last batch is not dropped when perfectly divisible**'    )
  UpperCAmelCase    				=						Accelerator()
  test_torch_metrics(SCREAMING_SNAKE_CASE							, 512    )
  accelerator.state._reset_state()


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : List[Any]    ):
  # For xla_spawn (TPUs)
  main()


if __name__ == "__main__":
  main()





'''simple docstring'''




from __future__ import annotations


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : int | str    ):
  UpperCAmelCase    				=						str(SCREAMING_SNAKE_CASE    )
  return n == n[::-1]


def    lowerCamelCase__		(    SCREAMING_SNAKE_CASE   : int = 100_0000    ):
  UpperCAmelCase    				=						0

  for i in range(1							, SCREAMING_SNAKE_CASE    ):
    if is_palindrome(SCREAMING_SNAKE_CASE    ) and is_palindrome(bin(SCREAMING_SNAKE_CASE    ).split('b'    )[1]    ):
      total += i
  return total


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


