import Data.Maybe
import qualified Data.ByteString.Char8 as B
import qualified Data.Set as S
main = mapM_ (print . solve) . analyze . map readInt . B.words =<< B.getContents
readInt = fst . fromJust . B.readInt
analyze (n:k:cs) = let (as,bs) = splitAt n cs in (n,k,as):analyze bs
analyze _ = []
solve (n,k,as) = fst . foldr f (0,(0,S.empty)) . zip [n,n-1..] $ as where
  f (i,a) (c,(d,s)) = (c+r,(e,t)) where
    e = k-a+d
    t = S.insert (d,i) s
    r = case S.lookupGE (e,0) t of
      Just x  -> i-S.findIndex x t
      Nothing -> 0