17.88 ft

i dont know how to optimise - but instead i used a cartesian plane and found that a gradient of 2 yielded the shortest distance for a line that passes through (4,8) which represents the top of the fence.

hope this type of thinking could help

im not sure if i am correct so please correct me if i am wrong