Try it online!
I'm very proud of this answer. IMO, one of my best brain-flak golfs.
How does it work?
As many other users have pointed out, the answer is simply sqrt(n) * 2. However, calculating the square root in brain-flak is very very nontrivial. Since we know the input will always be a square, we can optimize. So we write a loop that subtracts
1, 3, 5, 7, 9...
from the input, and track how many times it runs. Once it hits 0, the answer is simply the last number we subtracted minus one.
Originally, I had pushed a counter on to the other stack. However, we can use the main stack itself as a counter, by increasing the stack height.
#While TOS (top of stack, e.g. input) != 0:
#The negative of the height of the stack (since we're subtracting)
#Plus the TOS pushed twice. This is like incrementing a counter by two
#Pop one value off the main stack (or in other words, decrement our stack-counter)
#And push the height of the stack onto the alternate stack
In python-y pseudocode, this is basically the following algorithm:
l = [input]
while l[-1] != 0: #While the back of the list is nonzero
old_len = len(l)
l.append(l[-1] - old_len)