Let $k$ and $n$ be positive integers, with $k \geq 2$. In a straight line there are $kn$ stones of $k$ colours, such that there are $n$ stones of each colour. A [i]step[/i] consists of exchanging the position of two adjacent stones. Find the smallest positive integer $m$ such that it is always possible to achieve, with at most $m$ steps, that the $n$ stones are together, if:
a) $n$ is even.
b) $n$ is odd and $k=3$
**
   - \( m_1 = 0 \).
   - Solving the recurrence: 
     \[
     m_k = \sum_{i=1}^{k-1} \frac{n^2i}{2} = \frac{n^2}{2} \sum_{i=1}^{k-1} i = \frac{n^2}{2} \cdot \frac{(k-1)k}{2} = \frac{n^2k(k-1)}{4}
     \]

6. **Verification:**
   - Consider a line with \( k \) colors and \( n \) stones per color arranged as:
     \[
     \frac{n}{2} \text{ stones of } A_1, \frac{n}{2} \text{ stones of } A_2, \ldots, n \text{ stones of } A_k, \ldots, \frac{n}{2} \text{ stones of } A_1
     \]
   - Each color needs \( \frac{n^2(k-1)}{2} \) steps to get to an end.
   - Summing up for all colors:
     \[
     \frac{n^2(k-1)}{2} + \frac{n^2(k-2)}{2} + \ldots + \frac{n^2}{2} = \frac{n^2k(k-1)}{4}
     \]

### Part (b): \( n \) is odd and \( k = 3 \)

1. **Define the Problem:**
   - We have 3 colors and \( n = 2q + 1 \) stones per color.

2. **Step Analysis:**
   - Moving one color to an end requires \( n^2 \) steps.
   - Arranging the remaining stones in Order 2\(_i\) requires \( \lfloor \frac{n^2}{2} \rfloor = 2q^2 + 2q \) steps.

3. **Total Steps:**
   - Total steps required: \( n^2 + 2q(q+1) \).

4. **Verification:**
   - Consider a specific arrangement:
     \[
     q \text{ stones of } 1, q-1 \text{ stones of } 2, q-1 \text{ stones of } 3, 1 \text{ stone of } 2, 2 \text{ stones of } 3, 1 \text{ stone of } 1, 2 \text{ stones of } 2, q \text{ stones of } 3, q-1 \text{ stones of } 2, q \text{ stones of } 1
     \]
   - Calculate steps for moving color 1 to an end and arranging the rest.
   - Calculate steps for moving color 2 to an end and arranging the rest.
   - Both scenarios confirm \( n^2 + 2q(q+1) \) steps.

The final answer is \( \boxed{\frac{n^2k(k-1)}{4}} \) for part (a) and \( \boxed{n^2 + 2q(q+1)} \) for part (b).