18. (NOR) Let $a, b$ be natural numbers with $1 \leq a \leq b$, and $M=\left[\frac{a+b}{2}\right]$. Define the function $f: \mathbb{Z} \rightarrow \mathbb{Z}$ by  $$ f(n)= \begin{cases}n+a, & \text { if } n<M \\ n-b, & \text { if } n \geq M\end{cases} $$  Let $f^{1}(n)=f(n), f^{i+1}(n)=f\left(f^{i}(n)\right), i=1,2, \ldots$. Find the smallest natural number $k$ such that $f^{k}(0)=0$.
18. Clearly, it suffices to consider the case $(a, b)=1$. Let $S$ be the set of integers such that $M-b \leq x \leq M+a-1$. Then $f(S) \subseteq S$ and $0 \in S$. Consequently, $f^{k}(0) \in S$. Let us assume for $k>0$ that $f^{k}(0)=0$. Since $f(m)=m+a$ or $f(m)=m-b$, it follows that $k$ can be written as $k=r+s$, where $r a-s b=0$. Since $a$ and $b$ are relatively prime, it follows that $k \geq a+b$. Let us now prove that $f^{a+b}(0)=0$. In this case $a+b=r+s$ and hence $f^{a+b}(0)=(a+b-s) a-s b=(a+b)(a-s)$. Since $a+b \mid f^{a+b}(0)$ and $f^{a+b}(0) \in S$, it follows that $f^{a+b}(0)=0$. Thus for $(a, b)=1$ it follows that $k=a+b$. For other $a$ and $b$ we have $k=\frac{a+b}{(a, b)}$.