#include <bits/stdc++.h>
using namespace std;
bool myfunc(char ch1, char ch2) { return ch1 < ch2; }
long long arr[1000][1000], x, y;
int main() {
  long long t, k, l, r, sum = 0, M = 1e5;
  cin >> t >> k;
  while (t--) {
    cin >> l >> r;
    sum += r - l + 1;
    if (l < M) M = l;
  }
  cout << (k - (sum % k)) % k;
}
