#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, x, y, sum = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    sum += y - x + 1;
  }
  cout << ceil((float)sum / m) * m - sum;
  return 0;
}
