Using ratio
Up the hill and down the hill was the same distance each way
Uphill: 6 km would take 2 hours Downhill: 6 km would take 1 hour
Total: 12 km would take 3 hours = 4 km per hour
Average speed for whole journey = 4 km per hour
4 km per hour $\times$ 2 hours = 8 km
Using algebra
If the distance along the flat was $f$ km and the distance up the hill was $h$ km, then since $Speed = \frac{Distance}{Time} \Rightarrow Time = \frac{Distance}{Speed}$ then the total time taken was: $\frac{f}{4} + \frac{h}{3} + \frac{h}{6} + \frac{f}{4} = \frac{f}{2} + \frac{h}{2} = 2$.
Then, doubling this gives $f+h = 4$.
Therefore the total distance walked is $2(f+h) = 2 \times 4 = 8\text{km}$