#include <bits/stdc++.h>
using namespace std;
long long min(int a, long long b) {
  if (a < b)
    return a;
  else
    return b;
}
long long min(long long a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
long long min(long long a, long long b) {
  if (a < b)
    return a;
  else
    return b;
}
long long max(int a, long long b) {
  if (a > b)
    return a;
  else
    return b;
}
long long max(long long a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
long long max(long long a, long long b) {
  if (a > b)
    return a;
  else
    return b;
}
long long binpow(long long a, long long n) {
  if (n == 0) return 1;
  if (n % 2 == 1)
    return a * binpow(a, n - 1);
  else
    return binpow(a * a, n / 2);
}
void solve() {
  long long n, k;
  cin >> n >> k;
  long long a[n], b[n], sum1 = 0, sum2 = 0;
  vector<bool> r(k, false);
  r[0] = true;
  for (long long i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    sum1 += a[i];
    sum2 += b[i];
    long long minsdv = max(1, k - b[i]), maxsdv = min(k - 1, a[i]);
    vector<bool> newr(k, false);
    for (long long j = 0; j < k; j++) {
      if (!r[j]) continue;
      for (long long q = minsdv; q <= maxsdv; q++) newr[(j + q) % k] = true;
    }
    for (long long j = 0; j < k; j++) r[j] = r[j] || newr[j];
  }
  long long sum = sum1 + sum2;
  for (long long i = 0; i < k; i++) {
    if (!r[i]) continue;
    if (((sum2 + i) % k + (sum - sum2 + k - i) % k) < k) {
      cout << sum / k;
      return;
    }
  }
  cout << sum1 / k + sum2 / k;
}
signed main() {
  long long t;
  t = 1;
  while (t--) solve();
}
