#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int mx = 0, mn = 1000001;
  int vis[1000000] = {0};
  int c = 0;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    vis[a] = 1;
    vis[b] = 1;
    if (a > mx) {
      mx = a;
    }
    c += (b - a) + 1;
  }
  if (c % k != 0) {
    c %= k;
    c = k - c;
  } else {
    cout << 0 << endl;
    return 0;
  }
  cout << c << endl;
  return 0;
}
