Python 3, 104 bytes
from math import floor,sqrt
def a(n):x=(floor(sqrt(n-1))+1)//2;return 8*x**2+(6,-2)[n<=4*x**2+2*x]*x+2-n
Python 3, 104 bytes
from math import floor,sqrt
def a(n):x=(floor(sqrt(n-1))+1)//2;return 8*x**2+(6,-2)[n<=4*x**2+2*x]*x+2-n