Number theory tricky Let $f:\mathbb{N}\to\mathbb{N}$
be defined as follows: for every natural number $m$, $f(m)$ is the smallest natural number $k$ such that there exists a sequence $m=a_1<a_2<...<a_t=k$ for which $a_1 \cdot a_2 \cdot \cdots \cdot a_t$ is a square. 
Prove: $f$ is injective, and its range is exactly all of the composite numbers.
I've found that for prime numbers $p$ we have $f(p)=2p$, so it's injective on prime numbers. But I have no idea about the rest.
 A: Let us first show that $f$ is injective.
Suppose to the contrary that $f$ is not. To be precise, suppose that $f(m_1) = f(m_2) = n$ and $m_1 < m_2$. By the hypothesis there are sequences $\{m_1^0 = m_1, \ m_1^1 \cdots, \  m_1^{l_1} = n\}$ and $\{m_2^0 = m_1, \ m_2^1 \cdots, \  m_2^{l_2} = n\}$ such that 
$$
\begin{aligned}
\prod_{i=0}^{l_1} m_1^i &= M_1^2;\\
\prod_{i=0}^{l_2} m_2^i &= M_2^2,
\end{aligned}
$$
for some $M_1, M_2 \in \mathbb N$, and no other such sequence starting with $m_1$ 
 or $m_2$ terminates with smaller $n$.
Construct a new sequence by joining the two sequence together, arrange them in increasing order, and kill (both instances of) all duplicated terms. For instance, if the original sequences are $\{2, 8\}$ and $\{3, 6, 8\}$, then the new sequence will be $\{2, 3, 6, \not 8, \not 8\} = \{2,3,6\}$.
It is easy to see that the new sequence starts with $m_1$, terminates with something strictly less than $n$, and its product is a square, hence we arrive at a contradiction.
Now we show that the range of $f$ is exactly the composite numbers. Here I want to point out that since $f(1) = 1$, you need to consider $1$ as composite.
Let $n > 1$ be a composite number. For brevity suppose $n$ is not a square. We need to find a number $m$ such that $f(m) = n$. Since $n$ is composite, we have $n = d_1 d_2$ for some $2 \leq d_1 < d_2$. So we have a sequence $\{d_1, d_2, n\}$ such that $d_1d_2n = n^2$. If $f(d_1) = n$, then we are done. If $f(d_1) = n' < n$, then we have a sequence $\{m_0 = d_1, \ m_1 \cdots, \  m_l = n'\}$ such that
$$
\prod_{i=0}^l m_i = M^2
$$
for some integer $M$.
Do the exclusive or (as in the first part) on the two sequences, and we will get a new sequence. The new sequence starts with something strictly larger than $d_1$, terminates with $n$, and the product of the sequence is a square. Repeat this process, and we will end up with some number $m$ such that $f(m) = n$.
Finally, as Mees de Vries suggested in the comment, we need to show that the primes are not in the range. This is true, since for any sequence $\{m_0, \cdots, m_l = p\}$ which ends at a prime, we know that $p$ divides the product $\prod_{i=0}^l m_i$, but $p^2$ does not, so the product cannot be a square.
