#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
bool is_vowel(char c) {
  c = tolower(c);
  return c == 'a' || c == 'e' || c == 'o' || c == 'i' || c == 'u' || c == 'y';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> a(n);
  long long d = 0;
  for (int i = 0, l, r; i < n; i++) {
    cin >> l >> r;
    a[i] = {l, r};
    d += r - l + 1;
  }
  cout << (d % k == 0 ? 0 : k - d % k);
  return 0;
}
