import Data.List

main :: IO ()
main = do
    n <- readLn :: IO Int
    ps <- map read . words <$> getLine :: IO [Int]
    print $ solve ps

solve ps =
    length $ filter f $ g ps
    where
        f (xs@[x, y, z]) = y == (sort xs) !! 1

        g (x : y : z : xs) = [x, y, z] : g (y : z : xs)
        g xs = []