#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    int te1, te2;
    cin >> te1 >> te2;
    cnt += te2 - te1 + 1;
  }
  cout << ((cnt + K - 1) / K) * K - cnt << endl;
  return 0;
}
