Count switches in a smallest square root sequence mod \$2^n\$ 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&amp;\text{if }\frac{x_k^2-a}{2^k}\text{ is even}\\x_k+2^{k-1}&amp;\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 -&gt; 0
9, 4 -&gt; 1
1, 8 -&gt; 0
9, 16 -&gt; 1
17, 24 -&gt; 13
25, 32 -&gt; 2
33, 40 -&gt; 18
41, 48 -&gt; 17
49, 56 -&gt; 1
1048577, 2048 -&gt; 959
1048585, 2048 -&gt; 970


Winning Condition

This is a code-golf challenge, so shortest valid submission of each language wins. Standard loopholes are forbidden by default.
05AB1E, 15 14 bytes

LoεLnαy%0k}üÊO


Try it online!

For each n, this finds the smallest square root of a by brute-force.

05AB1E, 26 23 bytes (fast)

13v¹yλ£DnIαN&gt;o%2÷+]@γg&lt;


Try it online!

This one properly uses Cook's formula.

13                    # literal 13
  v               ]   # for each digit:
   ¹                  #  push the input k
    y             ]   #  push the digit
     λ£               #  recurse k times with base case y:
       Dn             #   square of the current value
         Iα           #   absolute difference with input a
           N&gt;o%       #   modulo 2**(N+1)
               2÷     #   integer divide by 2
                 +    #   add to the current value
@                     # compare the two lists element-wise
 γ                    # group consecutive equal elements
  g                   # length
   &lt;                  # -1