function findDelay(firewall) {
let delay = 0;
const maxDepth = Math.max(...Object.keys(firewall).map(Number));
const nextCatch = Array(maxDepth + 1).fill(null);
for (const [depth, range] of Object.entries(firewall)) {
nextCatch[Number(depth)] = (range - 1) * 2;
}
while (true) {
let caught = false;
for (const [depth, period] of nextCatch.entries()) {
if (period !== null && (depth + delay) % period === 0) {
caught = true;
break;
}
}
if (!caught) {
return delay;
}
delay++;
}
}