#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
bool compare(T x, U y) {
  return (abs(x - y) <= 1e-9);
}
const int MOD = 1e9 + 7;
void solve() {
  int n, k;
  cin >> n >> k;
  int ans = 0;
  while (n--) {
    int x, y;
    cin >> x >> y;
    ans = (ans + y - x + 1) % k;
  }
  cout << (k - ans) % k;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  t = 1;
  while (t--) {
    solve();
    cout << "\n";
  }
}
