Navigating over a square spiral
I wrote my answer in c++, it might help you. On first look, our solutions are quite similar, at least from an idea standpoint.

//
// Created by Felix Crazzolara on 09.12.17.
//

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

using namespace std;

int main (int argc, char* argv[]) {
    int position = 277678;

    // Find spiral radius
    int n = 1;
    while (position &gt; n*n)
         n += 2;
    // Find side
    int side = 4;
    int pos = n*n;
    while (side &gt; 1 &amp;&amp; position &lt; (pos - n + 1)) {
        pos -= n + 1;
        --side;
    }
    int middle_point = pos - (n - 1) / 2;
    int dist = abs(position - middle_point);
    dist += (n - 1) / 2;
    cout &lt;&lt; dist &lt;&lt; endl;
}


Explanation:
The highest number in every spiral is the bottom right corner, that has the value \$n^2\$ if we denote the length of the sides of a spiral with \$n\$.

If a spiral has side length \$n\$ obviously the next spiral has side length \$n' = n + 2\$.


Let's assume that our point is in the spiral with side length \$n\$. There are 4 corners. Our point got to be somewhere, so we want to find a corner that is greater-equal our point. Once we got the "smallest" corner being greater-equal then our point, we know, that our point must lay on the side given by the found corner and the next smaller corner.


When starring a bit on the first numbers of the memory layout, once you'll remark that the way to go the origin is given by the way to go to the closest point being vertical/horizontal in respect to the origin, being on the current spiral, plus the way from that point to the origin.


How these ideas fit together in code, I'll leave up to you to think about.