#include <bits/stdc++.h>
using namespace std;
long long n, m, l, r, p;
inline long long Sum(long long num) { return num * (num + 1) / 2 % p; }
long long calc(long long l, long long r, long long num) {
  long long pl = (l + num - 1) / num;
  long long pr = r / num;
  return ((pr - pl + 1) * (n + 1) - num * (Sum(pr) - Sum(pl - 1))) % p;
}
int main() {
  cin >> n >> m >> l >> r >> p;
  long long ans = 0;
  long long lb = l;
  long long ub = n;
  for (long long w = 1; w <= m; w++) {
    while (lb > 1 && (lb - 1) * (lb - 1) + w * w >= l * l) lb--;
    while (ub > 0 && ub * ub + w * w > r * r) ub--;
    if (lb <= ub && lb <= n && ub > 0) {
      vector<long long> factors;
      long long num = w;
      for (long long i = 2; i * i <= num; i++) {
        if (num % i == 0) {
          factors.push_back(i);
          while (num % i == 0) num /= i;
        }
      }
      if (num > 1) factors.push_back(num);
      long long c = 0;
      for (int mask = (int)(0); mask < (int)(1 << factors.size()); mask++) {
        long long t = 1;
        long long s = 1;
        for (int i = (int)(0); i < (int)(factors.size()); i++) {
          if ((((mask) >> (i)) & 1)) {
            s *= -1;
            t *= factors[i];
          }
        }
        c += s * calc(lb, ub, t) % p + p;
        c %= p;
      }
      assert(0 <= c && c < p);
      ans += c * (m - w + 1);
      ans %= p;
    }
  }
  if (l <= 1 && 1 <= r)
    ans = (ans * 2 + (n + 1) * m + n * (m + 1)) % p;
  else
    ans = ans * 2 % p;
  cout << ans << endl;
  return 0;
}
