Each time the line crosses a lattice line it enters a new cell. The line starts at $(0,0)$ and ends at $(L-i,i)$. We ignore the final intersection at $(L-i,i)$ but not the first at $(0,0)$. It intersects $L-i$ vertical lines and $i$ horizontal lines, so $L$ in total. But when it passes through a point $(m,n)$ it crosses two lines at once, so we need to deduct the number of such points that it passes through. But that is just the gcd of $i$ and $L$.