diff --git a/buggy.py b/buggy.py
--- a/buggy.py
+++ b/buggy.py
@@ -10,7 +10,7 @@
 
 for i in range(1, 10**6 + 1):
     p = ceil(m / i)
-    if 1 <= p <= n and m <= i * p <= n**2:
+    if 1 <= p <= n and m <= i * p <= n**2 and 1 <= i <= n:
         ans = min(ans, i * p)
 if ans == n**2 + 2:
     print(-1)
