Problem:
Score : 300 points

Problem Statement

There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.

On this sequence, Snuke can perform the following operation:

Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.

Snuke would like to make all the elements in this sequence equal by repeating the operation above some number of times.
Find the minimum number of operations required.
It can be proved that, Under the constraints of this problem, this objective is always achievable.

Constraints

2 \leq K \leq N \leq 100000

A_1, A_2, ..., A_N is a permutation of 1, 2, ..., N.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of operations required.

Sample Input 1

4 3
2 3 1 4

Sample Output 1

2

One optimal strategy is as follows:

In the first operation, choose the first, second and third elements. The sequence A becomes 1, 1, 1, 4.

In the second operation, choose the second, third and fourth elements. The sequence A becomes 1, 1, 1, 1.

Sample Input 2

3 3
1 2 3

Sample Output 2

1

Sample Input 3

8 3
7 3 1 8 4 6 2 5

Sample Output 3

4

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000+7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Mfor(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, t, s, q, last, cnt, sum, ans, dp[800][800],a[2000000], b[2000000];
string str,ss;
bool f[800][800],flag[800],tdp[800][800];
char c;
int di[4][2] = { { 0,1 },{ 1,0 },{ -1,0 } ,{ 0,-1 } };
struct Edge { LL to, cost; };
vector<LL>vec[10000];
vector<Pll>v;
map<string, LL>ma;
set<LL>st;
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}
 
int BipartiteGraph(LL point, vector<int>&color) {
	int cnt = 0;
	function<bool(int, int, int)> dfs = [&](int u, int prev, int c) {
		color[u] = c;
		if (c == 1) cnt++;
		rep(v, n)if (v != prev && v != u && f[u][v] == 0) {
			if (color[v] == -1) {
				if (!dfs(v, u, 1 - c)) return false;
			}
			else if (color[v] != 1 - c) {
				return false;
			}
		}
		return true;
	};
	if (!dfs(point, -1, 0)) return -1;
	return cnt;
}
int main() {
	ans = INF;
	cin >> n >> m;
	rep(i, m) {
		LL x, y;
		cin >> x >> y;
		x--, y--;
		f[x][y] = 1;
		f[y][x] = 1;
	}
	rep(i, n) {
		if (flag[i] == 0) {
			vector<int> color(n, -1);
			if (BipartiteGraph(i, color) == -1) {
				cout << -1 << endl;
				return 0;
			}
			cnt = 0, sum = 0;
			rep(j, n) {
				if (color[j] != -1) {
					flag[j] = 1;
					if (color[j] == 1)
						cnt++;
					sum++;
				}
			}
			v.push_back(Pll(sum - cnt, cnt));
		}
	}
	tdp[0][0] = 1;
	rep(i, v.size()) {
		Mfor(j, n) {
			if (tdp[i][j])
				tdp[i+1][j + v[i].fir] = 1, tdp[i+1][j + v[i].sec] = 1;
		}
	}
	rep(i, n) {
		if (tdp[v.size()][i]&&i) {
			ans = min(ans, i*(i - 1) / 2 + (n - i)*(n - i - 1) / 2);
		}
	}
	cout << ans << endl;
	return 0;
}