# CJam, 18 bytes

    T2/(f.-~(+.*:-z.5*

[Try it online.][1]

[1]: http://cjam.aditsu.net/#code=q~%3AT%3B%20e%23%20Store%20input%20in%20T.%0A%0AT2%2F(f.-~(%2B.*%3A-z.5*&input=%5B1%202%204%202%203%207%5D