#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
inline long long getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
inline long long add(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline long long sub(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline long long mul(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1LL;
  long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = mul(_ret, _a, _mod);
  return _ret;
}
long long mymul(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 0LL;
  long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = add(_ret, _a, _mod);
  return _ret;
}
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
long long st[101010 << 2], tg[101010 << 2];
void push(int no, int l, int r) {
  if (tg[no] == 0) return;
  st[(no << 1)] += (((l + r) >> 1) - l + 1) * tg[no];
  tg[(no << 1)] += tg[no];
  st[(1 + (no << 1))] += (r - ((l + r) >> 1)) * tg[no];
  tg[(1 + (no << 1))] += tg[no];
  tg[no] = 0;
}
void modify(int no, int l, int r, int ql, int qr) {
  if (r < ql or l > qr) return;
  if (ql <= l and r <= qr) {
    st[no] += r - l + 1;
    tg[no]++;
    return;
  }
  push(no, l, r);
  modify((no << 1), l, ((l + r) >> 1), ql, qr);
  modify((1 + (no << 1)), ((l + r) >> 1) + 1, r, ql, qr);
  st[no] = st[(no << 1)] + st[(1 + (no << 1))];
}
long long query(int no, int l, int r, int p) {
  if (l == r) return st[no];
  push(no, l, r);
  if (p <= ((l + r) >> 1)) return query((no << 1), l, ((l + r) >> 1), p);
  return query((1 + (no << 1)), ((l + r) >> 1) + 1, r, p);
}
void build() {}
int n, a[101010];
vector<int> pos[101010];
vector<int> cand;
void init() {
  n = getint();
  for (int i = 1; i <= n; i++) {
    a[i] = getint();
    pos[a[i]].push_back(i);
    cand.push_back(a[i]);
  }
  sort(cand.begin(), cand.end());
  cand.resize(unique(cand.begin(), cand.end()) - cand.begin());
}
int go_find(int ql, int qr, int who) {
  if (ql > qr) return -1;
  int iterl =
      lower_bound(pos[who].begin(), pos[who].end(), ql) - pos[who].begin();
  if (iterl == (int)pos[who].size()) return -1;
  if (pos[who][iterl] > qr) return -1;
  int iterr =
      upper_bound(pos[who].begin(), pos[who].end(), qr) - pos[who].begin();
  return pos[who][iterr - 1];
}
void mod(int ql, int qr) {
  if (ql > n) ql -= n;
  if (ql <= qr)
    modify(1, 1, n, ql, qr);
  else {
    modify(1, 1, n, ql, n);
    modify(1, 1, n, 1, qr);
  }
}
void solve() {
  long long ans = n, pre = 0;
  for (auto i : cand) {
    int pr = go_find(pre + 1, n, i);
    int pl = go_find(1, pre - 1, i);
    for (auto pp : pos[i]) ans += query(1, 1, n, pp);
    if (pl == -1 and pr == -1)
      assert(false);
    else if (pl == -1 or pr == -1) {
      mod(pre + 1, max(pl, pr));
      pre = max(pl, pr);
    } else {
      mod(pre + 1, pl);
      pre = pl;
    }
  }
  cout << ans << endl;
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}
