#include <bits/stdc++.h>
using namespace std;
struct o {
  int x;
  int y;
};
bool comp(o a, o b) { return a.x > b.x; }
long long n, m;
o a[1000000] = {0};
int main() {
  cin >> n >> m;
  for (int k = 1; k <= n; ++k) cin >> a[k].x >> a[k].y;
  long s = 0;
  for (int k = 1; k <= n; ++k) {
    s = s + abs(a[k].y - a[k].x + 1);
  }
  if (s % m == 0)
    cout << 0;
  else {
    long j = 0;
    j = s / m;
    cout << m * (s / m + 1) - s;
  }
  return 0;
}
