#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
  cin.sync_with_stdio(false);
  cout << setprecision(16);
  long long n, k, sum = 0;
  cin >> n >> k;
  for (int i = 0; i < (int)(n); i++) {
    long long a, b;
    cin >> a >> b;
    sum += b - a + 1;
  }
  sum %= k;
  if (sum) sum = k - sum;
  cout << sum;
  cout.flush();
}
