Definition
For any \$a\equiv1\ (\text{mod }8)\$ and \$n\ge3\$, there are exactly 4 roots to the equation \$x^2\equiv a\ (\text{mod }2^n)\$. Now, let \$x_k(a)\$ be the smallest root to the equation \$x^2\equiv a\ (\text{mod }2^k)\$, then $$\{x_3(a),x_4(a),x_5(a),x_6(a),\cdots\}$$ is a smallest square root sequence (SSRS) of \$a\$ mod \$2^n\$.
John D. Cook published a quick algorithm that calculates such roots in \$O(n)\$ time. Assume \$x_k\$ is a root to the equation \$x^2\equiv a\ (\text{mod }2^k)\$. Then, $$x_{k+1}=\begin{cases}x_k&\text{if }\frac{x_k^2-a}{2^k}\text{ is even}\\x_k+2^{k-1}&\text{otherwise}\end{cases}$$ is a root to the equation \$x^2\equiv a\ (\text{mod }2^{k+1})\$.
Now we define two lists A and B. \$A=\{A_k|k\ge3\}\$ is the list of values generated by the algorithm above with initial values \$A_3=1\$ and \$B=\{B_k|k\ge3\}\$ is the list of values generated with initial values \$B_3=3\$. Each entry in the SSRS \$x_k(a)\$ takes the smallest value among \$A_k\$ and \$B_k\$. We say a switch in SSRS occurs whenever the choice changes from A to B or from B to A.
To illustrate the definition, take \$a=17\$:
The smallest numbers are highlighted. From the picture there are 13 switches up to mod \$2^{24}\$.
Challenge
Write a function or program, that receives 2 integers \$a,\ k\$ as input (where \$a\equiv1\ (\text{mod }8)\$ and \$k\ge3\$) and output how many switches occur in the SSRS of \$a\$ mod \$2^n\$ up to \$n=k\$.
Sample I/O
1, 3 -> 0
9, 4 -> 1
1, 8 -> 0
9, 16 -> 1
17, 24 -> 13
25, 32 -> 2
33, 40 -> 18
41, 48 -> 17
49, 56 -> 1
1048577, 2048 -> 959
1048585, 2048 -> 970
Winning Condition
This is a code-golf challenge, so shortest valid submission of each language wins. Standard loopholes are forbidden by default.
1048577, 2048 --> 959
with all the other test cases correct. Am I missing something? \$\endgroup\$