



import pickle

import numpy as np
from matplotlib import pyplot as plt

class  snake_case__:

					"""simple docstring"""






					def __init__(				self   :      Optional[int] ,  SCREAMING_SNAKE_CASE   :      Union[str, Any] ,  SCREAMING_SNAKE_CASE   :      List[Any] ,  SCREAMING_SNAKE_CASE   :      Any ,  SCREAMING_SNAKE_CASE   :      Tuple ,  SCREAMING_SNAKE_CASE   :      List[Any] ,  SCREAMING_SNAKE_CASE   :      str=0.2 ,  SCREAMING_SNAKE_CASE   :      int=0.2	):
									lowercase__	:       str						=	bp_numa
									lowercase__	:       List[str]						=	bp_numa
									lowercase__	:       List[str]						=	bp_numa
									lowercase__	:       Union[str, Any]						=	conva_get[:2]
									lowercase__	:       Any						=	conva_get[2]
									lowercase__	:       Dict						=	size_pa
									lowercase__	:       Dict						=	rate_w
									lowercase__	:       int						=	rate_t
									lowercase__	:       Union[str, Any]						=	[
									    np.mat(-1 * np.random.rand(self.conva[0] ,  self.conva[0]	) + 0.5	)
									    for i in range(self.conva[1]	)
									]
									lowercase__	:       Optional[int]						=	np.mat(-1 * np.random.rand(self.num_bpa ,  self.num_bpa	) + 0.5	)
									lowercase__	:       Any						=	np.mat(-1 * np.random.rand(self.num_bpa ,  self.num_bpa	) + 0.5	)
									lowercase__	:       Dict						=	-2 * np.random.rand(self.conva[1]	) + 1
									lowercase__	:       Dict						=	-2 * np.random.rand(self.num_bpa	) + 1
									lowercase__	:       Optional[int]						=	-2 * np.random.rand(self.num_bpa	) + 1







					def 					snake_case	(				self   :      List[Any] ,  SCREAMING_SNAKE_CASE   :      Union[str, Any]	):
									lowercase__	:       List[str]						=	{
									    'num_bp1': self.num_bpa,
									    'num_bp2': self.num_bpa,
									    'num_bp3': self.num_bpa,
									    'conv1': self.conva,
									    'step_conv1': self.step_conva,
									    'size_pooling1': self.size_poolinga,
									    'rate_weight': self.rate_weight,
									    'rate_thre': self.rate_thre,
									    'w_conv1': self.w_conva,
									    'wkj': self.wkj,
									    'vji': self.vji,
									    'thre_conv1': self.thre_conva,
									    'thre_bp2': self.thre_bpa,
									    'thre_bp3': self.thre_bpa,
									}
									with open(SCREAMING_SNAKE_CASE ,  "wb"	) as f:
													pickle.dump(SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE	)

									print(f"""Model saved： {save_path}"""	)







					@classmethod
					def 					snake_case	(				cls   :      Any ,  SCREAMING_SNAKE_CASE   :      str	):

									with open(SCREAMING_SNAKE_CASE ,  "rb"	) as f:
													lowercase__	:       Union[str, Any]						=	pickle.load(SCREAMING_SNAKE_CASE	)  # noqa: S301

									lowercase__	:       int						=	model_dic.get("conv1"	)
									conv_get.append(model_dic.get("step_conv1"	)	)
									lowercase__	:       Optional[int]						=	model_dic.get("size_pooling1"	)
									lowercase__	:       Union[str, Any]						=	model_dic.get("num_bp1"	)
									lowercase__	:       Union[str, Any]						=	model_dic.get("num_bp2"	)
									lowercase__	:       Tuple						=	model_dic.get("num_bp3"	)
									lowercase__	:       int						=	model_dic.get("rate_weight"	)
									lowercase__	:       List[str]						=	model_dic.get("rate_thre"	)
									# create model instance
									lowercase__	:       List[Any]						=	CNN(SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE	)
									# modify model parameter
									lowercase__	:       Optional[Any]						=	model_dic.get("w_conv1"	)
									lowercase__	:       int						=	model_dic.get("wkj"	)
									lowercase__	:       Dict						=	model_dic.get("vji"	)
									lowercase__	:       Optional[Any]						=	model_dic.get("thre_conv1"	)
									lowercase__	:       List[Any]						=	model_dic.get("thre_bp2"	)
									lowercase__	:       Any						=	model_dic.get("thre_bp3"	)
									return conv_ins







					def 					snake_case	(				self   :      Any ,  SCREAMING_SNAKE_CASE   :      Optional[int]	):
									return 1 / (1 + np.exp(-1 * x	))







					def 					snake_case	(				self   :      List[str] ,  SCREAMING_SNAKE_CASE   :      Dict	):
									return round(SCREAMING_SNAKE_CASE ,  3	)







					def 					snake_case	(				self   :      Dict ,  SCREAMING_SNAKE_CASE   :      int ,  SCREAMING_SNAKE_CASE   :      Optional[int] ,  SCREAMING_SNAKE_CASE   :      Optional[int] ,  SCREAMING_SNAKE_CASE   :      int ,  SCREAMING_SNAKE_CASE   :      List[Any]	):
									lowercase__	:       Tuple						=	convs[0]
									lowercase__	:       Any						=	convs[1]
									lowercase__	:       Optional[Any]						=	np.shape(SCREAMING_SNAKE_CASE	)[0]
									# get the data slice of original image data, data_focus
									lowercase__	:       str						=	[]
									for i_focus in range(0 ,  size_data - size_conv + 1 ,  SCREAMING_SNAKE_CASE	):
													for j_focus in range(0 ,  size_data - size_conv + 1 ,  SCREAMING_SNAKE_CASE	):
																	lowercase__	:       Union[str, Any]						=	data[
																	    i_focus : i_focus + size_conv, j_focus : j_focus + size_conv
																	]
																	data_focus.append(SCREAMING_SNAKE_CASE	)
        # calculate the feature map of every single kernel, and saved as list of matrix
									lowercase__	:       List[Any]						=	[]
									lowercase__	:       str						=	int((size_data - size_conv) / conv_step + 1	)
									for i_map in range(SCREAMING_SNAKE_CASE	):
													lowercase__	:       Any						=	[]
													for i_focus in range(len(SCREAMING_SNAKE_CASE	)	):
																	lowercase__	:       Dict						=	(
																	    np.sum(np.multiply(data_focus[i_focus] ,  w_convs[i_map]	)	)
																	    - thre_convs[i_map]
																	)
																	featuremap.append(self.sig(SCREAMING_SNAKE_CASE	)	)
													lowercase__	:       Optional[Any]						=	np.asmatrix(SCREAMING_SNAKE_CASE	).reshape(
													    SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE	)
													data_featuremap.append(SCREAMING_SNAKE_CASE	)

									# expanding the data slice to One dimenssion
									lowercase__	:       str						=	[]
									for each_focus in data_focus:
													focusa_list.extend(self.Expand_Mat(SCREAMING_SNAKE_CASE	)	)
									lowercase__	:       Any						=	np.asarray(SCREAMING_SNAKE_CASE	)
									return focus_list, data_featuremap







					def 					snake_case	(				self   :      Any ,  SCREAMING_SNAKE_CASE   :      List[Any] ,  SCREAMING_SNAKE_CASE   :      Optional[Any] ,  SCREAMING_SNAKE_CASE   :      List[str]="average_pool"	):
									lowercase__	:       Tuple						=	len(featuremaps[0]	)
									lowercase__	:       List[Any]						=	int(size_map / size_pooling	)
									lowercase__	:       Optional[int]						=	[]
									for i_map in range(len(SCREAMING_SNAKE_CASE	)	):
													lowercase__	:       Union[str, Any]						=	featuremaps[i_map]
													lowercase__	:       Union[str, Any]						=	[]
													for i_focus in range(0 ,  SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE	):
																	for j_focus in range(0 ,  SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE	):
																					lowercase__	:       List[Any]						=	feature_map[
																					    i_focus : i_focus + size_pooling,
																					    j_focus : j_focus + size_pooling,
																					]
																					if pooling_type == "average_pool":
																									# average pooling
																									map_pooled.append(np.average(SCREAMING_SNAKE_CASE	)	)
																					elif pooling_type == "max_pooling":
																									# max pooling
																									map_pooled.append(np.max(SCREAMING_SNAKE_CASE	)	)
													lowercase__	:       Tuple						=	np.asmatrix(SCREAMING_SNAKE_CASE	).reshape(SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE	)
													featuremap_pooled.append(SCREAMING_SNAKE_CASE	)
									return featuremap_pooled







					def 					snake_case	(				self   :      List[str] ,  SCREAMING_SNAKE_CASE   :      Tuple	):
									lowercase__	:       Optional[int]						=	[]
									for i in range(len(SCREAMING_SNAKE_CASE	)	):
													lowercase__	:       Optional[int]						=	np.shape(data[i]	)
													lowercase__	:       Optional[Any]						=	data[i].reshape(1 ,  shapes[0] * shapes[1]	)
													lowercase__	:       int						=	data_listed.getA().tolist()[0]
													data_expanded.extend(SCREAMING_SNAKE_CASE	)
									lowercase__	:       Tuple						=	np.asarray(SCREAMING_SNAKE_CASE	)
									return data_expanded







					def 					snake_case	(				self   :      str ,  SCREAMING_SNAKE_CASE   :      Dict	):
									lowercase__	:       Optional[int]						=	np.asarray(SCREAMING_SNAKE_CASE	)
									lowercase__	:       Optional[int]						=	np.shape(SCREAMING_SNAKE_CASE	)
									lowercase__	:       Any						=	data_mat.reshape(1 ,  shapes[0] * shapes[1]	)
									return data_expanded







					def 					snake_case	(				self   :      Optional[Any] ,  SCREAMING_SNAKE_CASE   :      List[str] ,  SCREAMING_SNAKE_CASE   :      Union[str, Any] ,  SCREAMING_SNAKE_CASE   :      int ,  SCREAMING_SNAKE_CASE   :      Any ,  SCREAMING_SNAKE_CASE   :      List[Any]	):
									lowercase__	:       Optional[int]						=	[]
									lowercase__	:       Dict						=	0
									for i_map in range(SCREAMING_SNAKE_CASE	):
													lowercase__	:       Tuple						=	np.ones((size_map, size_map)	)
													for i in range(0 ,  SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE	):
																	for j in range(0 ,  SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE	):
																					lowercase__	:       Optional[int]						=	pd_pool[
																					    i_pool
																					]
																					lowercase__	:       Dict						=	i_pool + 1
													lowercase__	:       List[Any]						=	np.multiply(
													    SCREAMING_SNAKE_CASE ,  np.multiply(out_map[i_map] ,  (1 - out_map[i_map])	)	)
													pd_all.append(SCREAMING_SNAKE_CASE	)
									return pd_all







					def 					snake_case	(				self   :      Optional[Any] ,  SCREAMING_SNAKE_CASE   :      List[str] ,  SCREAMING_SNAKE_CASE   :      Optional[int] ,  SCREAMING_SNAKE_CASE   :      int ,  SCREAMING_SNAKE_CASE   :      Dict ,  SCREAMING_SNAKE_CASE   :      Union[str, Any] ,  SCREAMING_SNAKE_CASE   :      Optional[Any]=bool	):
									print("----------------------Start Training-------------------------"	)
									print((" - - Shape: Train_Data  ", np.shape(SCREAMING_SNAKE_CASE	))	)
									print((" - - Shape: Teach_Data  ", np.shape(SCREAMING_SNAKE_CASE	))	)
									lowercase__	:       Dict						=	0
									lowercase__	:       Tuple						=	[]
									lowercase__	:       List[Any]						=	10_000
									while rp < n_repeat and mse >= error_accuracy:
													lowercase__	:       List[Any]						=	0
													print(f"""-------------Learning Time {rp}--------------"""	)
													for p in range(len(SCREAMING_SNAKE_CASE	)	):
																	# print('------------Learning Image: %d--------------'%p)
																	lowercase__	:       Optional[int]						=	np.asmatrix(datas_train[p]	)
																	lowercase__	:       str						=	np.asarray(datas_teach[p]	)
																	lowercase__	:       List[Any]						=	self.convolute(
																	    SCREAMING_SNAKE_CASE ,  self.conva ,  self.w_conva ,  self.thre_conva ,  conv_step=self.step_conva ,  )
																	lowercase__	:       Optional[int]						=	self.pooling(SCREAMING_SNAKE_CASE ,  self.size_poolinga	)
																	lowercase__	:       List[str]						=	np.shape(SCREAMING_SNAKE_CASE	)
																	lowercase__	:       Optional[Any]						=	self._expand(SCREAMING_SNAKE_CASE	)
																	lowercase__	:       Dict						=	data_bp_input

																	lowercase__	:       Dict						=	np.dot(SCREAMING_SNAKE_CASE ,  self.vji.T	) - self.thre_bpa
																	lowercase__	:       Dict						=	self.sig(SCREAMING_SNAKE_CASE	)
																	lowercase__	:       int						=	np.dot(SCREAMING_SNAKE_CASE ,  self.wkj.T	) - self.thre_bpa
																	lowercase__	:       Tuple						=	self.sig(SCREAMING_SNAKE_CASE	)

																	# --------------Model Leaning ------------------------
																	# calculate error and gradient---------------
																	lowercase__	:       Dict						=	np.multiply(
																	    (data_teach - bp_outa) ,  np.multiply(SCREAMING_SNAKE_CASE ,  (1 - bp_outa)	)	)
																	lowercase__	:       Optional[int]						=	np.multiply(
																	    np.dot(SCREAMING_SNAKE_CASE ,  self.wkj	) ,  np.multiply(SCREAMING_SNAKE_CASE ,  (1 - bp_outa)	)	)
																	lowercase__	:       Optional[Any]						=	np.dot(SCREAMING_SNAKE_CASE ,  self.vji	)

																	lowercase__	:       List[Any]						=	pd_i_all / (self.size_poolinga * self.size_poolinga)
																	lowercase__	:       int						=	pd_conva_pooled.T.getA().tolist()
																	lowercase__	:       Dict						=	self._calculate_gradient_from_pool(
																	    SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE ,  shape_featuremapa[0] ,  shape_featuremapa[1] ,  self.size_poolinga ,  )
																	# weight and threshold learning process---------
																	# convolution layer
																	for k_conv in range(self.conva[1]	):
																					lowercase__	:       Tuple						=	self._expand_mat(pd_conva_all[k_conv]	)
																					lowercase__	:       Optional[int]						=	self.rate_weight * np.dot(SCREAMING_SNAKE_CASE ,  SCREAMING_SNAKE_CASE	)

																					lowercase__	:       List[Any]						=	self.w_conva[k_conv] + delta_w.reshape(
																					    (self.conva[0], self.conva[0])	)

																					lowercase__	:       Dict						=	(
																					    self.thre_conva[k_conv]
																					    - np.sum(pd_conva_all[k_conv]	) * self.rate_thre
																					)
																	# all connected layer
																	lowercase__	:       str						=	self.wkj + pd_k_all.T * bp_outa * self.rate_weight
																	lowercase__	:       Optional[int]						=	self.vji + pd_j_all.T * bp_outa * self.rate_weight
																	lowercase__	:       Dict						=	self.thre_bpa - pd_k_all * self.rate_thre
																	lowercase__	:       List[str]						=	self.thre_bpa - pd_j_all * self.rate_thre
																	# calculate the sum error of all single image
																	lowercase__	:       Tuple						=	np.sum(abs(data_teach - bp_outa	)	)
																	error_count += errors
																	# print('   ----Teach      ',data_teach)
																	# print('   ----BP_output  ',bp_out3)
													lowercase__	:       List[Any]						=	rp + 1
													lowercase__	:       Any						=	error_count / patterns
													all_mse.append(SCREAMING_SNAKE_CASE	)

									def draw_error():
													lowercase__	:       List[Any]						=	[error_accuracy for i in range(int(n_repeat * 1.2	)	)]
													plt.plot(SCREAMING_SNAKE_CASE ,  "+-"	)
													plt.plot(SCREAMING_SNAKE_CASE ,  "r--"	)
													plt.xlabel("Learning Times"	)
													plt.ylabel("All_mse"	)
													plt.grid(SCREAMING_SNAKE_CASE ,  alpha=0.5	)
													plt.show()

									print("------------------Training Complished---------------------"	)
									print((" - - Training epoch: ", rp, f"""     - - Mse: {mse:.6f}""")	)
									if draw_e:
													draw_error()
									return mse







					def 					snake_case	(				self   :      Union[str, Any] ,  SCREAMING_SNAKE_CASE   :      Optional[Any]	):
									lowercase__	:       Optional[int]						=	[]
									print("-------------------Start Testing-------------------------"	)
									print((" - - Shape: Test_Data  ", np.shape(SCREAMING_SNAKE_CASE	))	)
									for p in range(len(SCREAMING_SNAKE_CASE	)	):
													lowercase__	:       str						=	np.asmatrix(datas_test[p]	)
													lowercase__	:       Dict						=	self.convolute(
													    SCREAMING_SNAKE_CASE ,  self.conva ,  self.w_conva ,  self.thre_conva ,  conv_step=self.step_conva ,  )
													lowercase__	:       Optional[Any]						=	self.pooling(SCREAMING_SNAKE_CASE ,  self.size_poolinga	)
													lowercase__	:       Union[str, Any]						=	self._expand(SCREAMING_SNAKE_CASE	)

													lowercase__	:       Optional[int]						=	data_bp_input
													lowercase__	:       Tuple						=	bp_outa * self.vji.T - self.thre_bpa
													lowercase__	:       Any						=	self.sig(SCREAMING_SNAKE_CASE	)
													lowercase__	:       str						=	bp_outa * self.wkj.T - self.thre_bpa
													lowercase__	:       Any						=	self.sig(SCREAMING_SNAKE_CASE	)
													produce_out.extend(bp_outa.getA().tolist()	)
									lowercase__	:       Union[str, Any]						=	[list(map(self.do_round ,  SCREAMING_SNAKE_CASE	)	) for each in produce_out]
									return np.asarray(SCREAMING_SNAKE_CASE	)







					def 					snake_case	(				self   :      List[str] ,  SCREAMING_SNAKE_CASE   :      Dict	):
									lowercase__	:       Dict						=	np.asmatrix(SCREAMING_SNAKE_CASE	)
									lowercase__	:       Optional[Any]						=	self.convolute(
									    SCREAMING_SNAKE_CASE ,  self.conva ,  self.w_conva ,  self.thre_conva ,  conv_step=self.step_conva ,  )
									lowercase__	:       Optional[Any]						=	self.pooling(SCREAMING_SNAKE_CASE ,  self.size_poolinga	)

									return data_conveda, data_pooleda


if __name__ == "__main__":
				pass








def 							UpperCamelCase_( lowerCamelCase_					)       ->		int:
    if n == 1 or not isinstance(lowerCamelCase_				,					lowerCamelCase_					):
        return 0
    elif n == 2:
        return 1
    else:
        _lowercase			:				List[str] 						=						[0, 1]
        for i in range(2				,					n + 1					):
            sequence.append(sequence[i - 1] + sequence[i - 2]					)

        return sequence[n]



def 							UpperCamelCase_( lowerCamelCase_					)       ->		int:
    _lowercase			:				Tuple 						=						0
    _lowercase			:				List[str] 						=						2

    while digits < n:
        index += 1
        _lowercase			:				Optional[int] 						=						len(str(fibonacci(lowerCamelCase_					)					)					)

    return index



def 							UpperCamelCase_( lowerCamelCase_ = 1000					)       ->		int:
    return fibonacci_digits_index(lowerCamelCase_					)


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


