#include <bits/stdc++.h>
using namespace std;
int main() {
  int vecs[100][3] = {0};
  int tot[3] = {0};
  int n, k;
  scanf("%d %d", &n, &k);
  int temp;
  int t;
  for (int i = 0; i <= n - 1; i++) {
    scanf("%d", &temp);
    t = i % k;
    temp++;
    tot[temp]++;
    (vecs[t][temp])++;
  }
  int mx = -1;
  int sub;
  for (int i = 0; i <= k - 1; i++) {
    sub = tot[0] - vecs[i][0] - tot[2] + vecs[i][2];
    if (sub < 0) sub *= -1;
    if (sub > mx) mx = sub;
  }
  printf("%d\n", mx);
  return 0;
}
