diff --git a/buggy.py b/buggy.py
--- a/buggy.py
+++ b/buggy.py
@@ -22,7 +22,7 @@
     for i in range(len(B)):
         k = max(next_x[i], next_y[i])
         if k != n + 1:
-            r += n - k
+            r += len(B) - k
     return r
 
 
