#include <bits/stdc++.h>
using namespace std;

class Debug {
private:
    static int NAME_LENGTH, MAX_WIDTH;
    static bool IS_MATRIX;
    static long long INF;
public:
    static void set_NAME_LENGTH(int l) { NAME_LENGTH = l; }
    static int get_NAME_LENGTH() { return NAME_LENGTH; }
    static void set_MAX_WIDTH(int w) { MAX_WIDTH = w; }
    static int get_MAX_WIDTH() { return MAX_WIDTH; }
    static void set_IS_MATRIX(bool f) { IS_MATRIX = f; }
    static bool get_IS_MATRIX() { return IS_MATRIX; }
    static void set_INF(long long i) { INF = i; }
    static long long get_INF() { return INF; }
};

int Debug::NAME_LENGTH = 0;
int Debug::MAX_WIDTH = 0;
bool Debug::IS_MATRIX = false;
long long Debug::INF = LLONG_MAX;

// Debug for basic type element
template <typename T>
int ELEMENT_WIDTH(T& e) {
    int width;
    stringstream ss;
    ss << e;
    width = ss.str().size();
    if (ss.str() == to_string(Debug::get_INF())) width = "INF"s.size();
    if (ss.str() == to_string(-Debug::get_INF())) width = "-INF"s.size();
    return width;
}

template <typename T>
ostream& operator<<(ostream& os, T* e) {
    stringstream ss;
    ss << *e;
    if (ss.str() == to_string(Debug::get_INF())) os << "INF"s;
    else if (ss.str() == to_string(-Debug::get_INF())) os << "-INF"s;
    else os << ss.str();
    return os;
}
ostream& operator<<(ostream& os, char* e) {
    os << *e;
    return os;
}
ostream& operator<<(ostream& os, const char* e) {
    string s = e;
    os << s;
    return os;
}

// Debug for pair
template <typename T, typename U>
int ELEMENT_WIDTH(pair<T, U>& p) {
    return ELEMENT_WIDTH(p.first) + ELEMENT_WIDTH(p.second) + "(, )"s.size();
}

template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>* p) {
    os << "("s << &((*p).first) << ", "s << &((*p).second) << ")"s;
    return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>* p) {
    pair<T, U> p_not_const = *p;
    os << &p_not_const;
    return os;
}

// Debug for tuple
template<int N, class Tuple>
int WIDTH_TUPLE(Tuple& t){
    return 0;
}
template<int N, class Tuple, class Head, class... Tail>
int WIDTH_TUPLE(Tuple& t) {
    int width = 0;
    if (N) width += ", "s.size();
    width += ELEMENT_WIDTH(get<N>(t)) + WIDTH_TUPLE<N + 1, Tuple, Tail...>(t);
    return width;
}
template<class... Ts>
int ELEMENT_WIDTH(tuple<Ts...>& t) {
    return WIDTH_TUPLE<0, tuple<Ts...>, Ts...>(t) + "()"s.size();
}

template<int N, class Tuple>
void OUT_TUPLE(ostream& os, Tuple* t){}
template<int N, class Tuple, class Head, class... Tail>
void OUT_TUPLE(ostream& os, Tuple* t) {
    if (N) os << ", "s;
    os << &get<N>(*t);
    OUT_TUPLE<N + 1, Tuple, Tail...>(os, &(*t));
}
template<class... Ts>
ostream& operator<<(ostream& os, tuple<Ts...>* t) {
    os << "("s;
    OUT_TUPLE<0, tuple<Ts...>, Ts...>(os, &(*t));
    os << ")"s;
    return os;
}
template<class... Ts>
ostream& operator<<(ostream& os, const tuple<Ts...>* t) {
    tuple<Ts...> t_not_const = *t;
    os << &t_not_const;
    return os;
}

// Debug for vector (1-dimensional)
template <typename T>
ostream& operator<<(ostream& os, vector<T>* v) {
    os << "["s;
    bool flg = true;
    for (auto e : *v) {
        os << (flg ? ""s : ", "s);
        if (Debug::get_IS_MATRIX()) {
            string space = "";
            for (int i = 0; i < Debug::get_MAX_WIDTH() - ELEMENT_WIDTH(e); ++i) space += " ";
            os << space;
        }
        os << &e;
        flg = false;
    }
    os << "]"s;
    return os;
}

// Debug for vector (2-dimensional)
template <typename T>
ostream& operator<<(ostream& os, vector<vector<T>>* v) {
    Debug::set_IS_MATRIX(true);
    int width = 0;
    for (unsigned int i = 0; i < (*v).size(); ++i) {
        for (unsigned int j = 0; j < ((*v)[i]).size(); ++j) {
            width = max(width, ELEMENT_WIDTH((*v)[i][j]));
        }
    }
    Debug::set_MAX_WIDTH(width);
    string space = "";
    for (int i = 0; i < Debug::get_NAME_LENGTH() + 7; ++i) space += " ";
    os << "["s;
    bool flg = true;
    for (auto e : *v) {
        os << (flg ? ""s : "\n" + space);
        os << &e;
        flg = false;
    }
    os << "]"s;
    Debug::set_IS_MATRIX(false);
    return os;
}

// Debug for set
template <typename T>
ostream& operator<<(ostream& os, set<T>* s) {
    os << "{"s;
    bool flg = true;
    for (auto e : *s) {
        os << (flg ? ""s : ", "s);
        os << &e;
        flg = false;
    }
    os << "}"s;
    return os;
}

// Debug for multiset
template <typename T>
ostream& operator<<(ostream& os, multiset<T>* s) {
    os << "{"s;
    bool flg = true;
    for (auto e : *s) {
        os << (flg ? ""s : ", "s);
        os << &e;
        flg = false;
    }
    os << "}"s;
    return os;
}

// Debug for map
template <typename T, typename U>
ostream& operator<<(ostream& os, map<T, U>* mp) {
    os << "{"s;
    bool flg = true;
    for (auto e : *mp) {
        os << (flg ? ""s : ", "s);
        os << &(e.first) << ": "s << &(e.second);
        flg = false;
    }
    os << "}"s;
    return os;
}

// Debug for queue
template <typename T>
ostream& operator<<(ostream& os, queue<T>* que) {
    queue<T> que_cp = *que;
    os << "["s;
    bool flg = true;
    while (!que_cp.empty()) {
        os << (flg ? ""s : ", "s);
        os << &(que_cp.front());
        que_cp.pop();
        flg = false;
    }
    os << "]"s;
    return os;
}

// Debug for priority-queue
template <typename T>
ostream& operator<<(ostream& os, priority_queue<T>* que) {
    priority_queue<T> que_cp = *que;
    os << "["s;
    bool flg = true;
    while (!que_cp.empty()) {
        os << (flg ? ""s : ", "s);
        os << &(que_cp.top());
        que_cp.pop();
        flg = false;
    }
    os << "]"s;
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, priority_queue<T, vector<T>, greater<T>>* que) {
    priority_queue<T, vector<T>, greater<T>> que_cp = *que;
    os << "["s;
    bool flg = true;
    while (!que_cp.empty()) {
        os << (flg ? ""s : ", "s);
        os << &(que_cp.top());
        que_cp.pop();
        flg = false;
    }
    os << "]"s;
    return os;
}

// Debug for deque
template <typename T>
ostream& operator<<(ostream& os, deque<T>* que) {
    os << "["s;
    bool flg = true;
    for (auto e : *que) {
        os << (flg ? ""s : ", "s);
        os << &e;
        flg = false;
    }
    os << "]"s;
    return os;
}

// Debug assistant
#define DUMP(...) INTERNAL_DUMP(#__VA_ARGS__, __VA_ARGS__)

void REMOVE_SPACE(string& s) {
    while (s[0] == ' ') {
        s = s.substr(1);
    }
    while (s[s.size() - 1] == ' ') {
        s = s.substr(0, s.size() - 1);
    }
}

pair<string, string> GET_NAME(string s) {
    int bracket = 0, idx = 0;
    while ((unsigned)idx < s.size() && !(s[idx] == ',' && bracket == 0)) {
        if (s[idx] == '(') ++bracket;
        else if (s[idx] == ')') --bracket;
        ++idx;
    }
    string other_names;
    if ((unsigned)idx == s.size()) {
        other_names = "";
    } else {
        other_names = s.substr(idx + 1);
    }
    string first_name = s.substr(0, idx);
    REMOVE_SPACE(first_name);
    REMOVE_SPACE(other_names);
    return pair<string, string>(first_name, other_names);
}

// Debug main
void INTERNAL_DUMP(string name) {}
template <class Head, class... Tail>
void INTERNAL_DUMP(string name, Head&& head, Tail&&... tail) {
    pair<string, string> p = GET_NAME(name);
    Debug::set_NAME_LENGTH(p.first.size());
    cerr << ">> "s << &(p.first) << " = "s << &head << endl;
	INTERNAL_DUMP(p.second, move(tail)...);
}

// End of Debug parts
//------------------------------------------------------------------------------


using ll = long long;

int main() {
    ll N, X, M;  cin >> N >> X >> M;
    vector<ll> rr(M);
    for (ll i = 0; i < M; ++i) rr[i] = i * i % M;

    // find loop
    int now = X;
    int loop_num = 0;
    ll loop_sum = 0;
    bool self_loop = false;
    vector<ll> used(M);
    while (true) {
        loop_sum += now;
        if (used[rr[now]]) {
            if (now == rr[now]) self_loop = true;
            ++loop_num;
            break;
        }
        used[now] = 1;
        now = rr[now];
        ++loop_num;
    }

    ll ans = 0;
    if (!self_loop) {
        ans += loop_sum * (N / loop_num);
        now = X;
        for (int i = 0; i < N % loop_num; ++i) {
            ans += now;
            now = rr[now];
        }
    }
    else {
        ans += loop_sum;
        ans += now * (N - loop_num);
    }

    cout << ans << endl;
    DUMP(loop_num, loop_sum);
}
