Let $S$ be the set of elements coprime to $n$.
The smallest number $\lambda(n)$ such that $a^{\lambda(n)} \equiv 1 \bmod n$ for all $a\in S$ is the smallest number that is a multiple of all of the orders of the elements of $S$.
In other words we want to show that there is an element whose order is the lcm of all the orders of $S$.
We can do this by showing this lemma:
If there is an element $s$ with order $a$ and an element $t$ with order $b$ then there is an element with order $\operatorname{lcm}(a,b)$.
Proof: First assume $a$ and $b$ are coprime, then $st$ has the desired order, because if $(st)^l \equiv 1 \bmod n$ then $s^l \equiv t^{-l}$ but if $t^{-l} \not \equiv 1 \bmod n$ then the order of $t^{-l}$ is a divisor of $b$ which cannot be the order of $s^l$ because the order of $s^l$ is a divisor of $a$, which is coprime to $b$. Therefore if $(st)^l \equiv 1 $ we must have $s^l \equiv 1$ and $t^{-l} \equiv 1$ which implies $l$ is a multiple of $a$ and $b$.
If $a$ and $b$ are not coprime then let $a = \prod\limits_{i=1}^ s p_i^{a_i}$ and $b = \prod\limits_{i=1}^s p_i^{b_i}$, and let $A$ be the set of indices with $a_i \geq b_i$.
Define $a'$ as $\prod\limits_{i\in A} p_i^{a_i}$ and $b'=\prod\limits_{i \not \in A} p_i^{b_i}$.
And now take $s'=s^{a/a'}$ and $t'=t^{b/b'}$and apply the previous result with $s',t',a'$ and $b'$.