#include <iostream>
#include <limits.h>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
using namespace std;
using ll = long long;
#pragma region 상하좌우 / 위치
const pair<int, int> dir[]
{
{ 1, 0 },
{ -1, 0 },
{ 0, -1 },
{ 0, 1 },
};
#define _y first
#define _x second
#pragma endregion
#pragma region 빠른 입출력
#define FAST_IO() \
{\
ios::sync_with_stdio(false);\
cin.tie(NULL); \
cout.tie(NULL); \
}\
#pragma endregion
const map<int, vector<vector<int>>> blocks
{
{1, {{ 0}, { 0, 0, 0, 0 }}},
{2, {{ 0, 0 }}},
{3, {{ 0, 0, 1 }, { 1, 0 }}},
{4, {{ 1, 0, 0 }, { 0, 1 }}},
{5, {{ 0, 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 0, 1 }}},
{6, {{ 0, 0, 0 }, { 0, 0 }, { 0, 1, 1 }, { 2, 0 }}},
{7, {{ 0, 0, 0 }, { 0, 2 }, { 1, 1, 0 }, { 0, 0 }}}
};
int main()
{
FAST_IO();
int c, p;
cin >> c >> p;
vector<int> board(c);
for (int i = 0; i < board.size(); i++)
cin >> board[i];
int ans = 0;
// 2차원 vector 접근
for (const auto& floor : blocks.at(p))
{
int idx = 0, size = floor.size();
for (int i = 0; i <= c - size; i++)
{
bool flag = true;
for (int j = i; j < i + size - 1; j++)
{
int now = floor[j - i];
int next = floor[j - i + 1];
if (now - next != board[j] - board[j + 1])
{
flag = false;
break;
}
}
ans += flag;
}
}
cout << ans;
}