#include <bits/stdc++.h>
using namespace std;
const double PI = 2.0 * acos(0.0);
const double EPS = 1e-6;
struct seg {
  int u, v;
  seg() {}
  seg(int u, int v) : u(u), v(v) {}
  inline bool operator<(const seg &s) const {
    if (v != s.v) return v < s.v;
    return u < s.u;
  }
};
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<seg> V(n);
  for (int i = 0; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    V[i] = seg(u, v);
  }
  sort(V.begin(), V.end());
  int cover = V[0].v - V[0].u + 1;
  int now = V[0].v;
  for (int i = 1; i < n; i++) {
    if (V[i].v > now) {
      if (now > V[i].u)
        cover += V[i].v - now;
      else
        cover += V[i].v - V[i].u + 1;
      now = V[i].v;
    }
  }
  printf("%d\n", (k - cover % k) % k);
  return 0;
}
